File: Autopilot_ssf.c

    1   /*
    2    * Autopilot_ssf.c
    3    *
    4    * Automatically generated s-function with I/O interface for:
    5    * Component: Autopilot
    6    * Component Simulink Path: Autopilot
    7    * Simulation Mode: SIL
    8    *
    9    */
   10   
   11   #define S_FUNCTION_NAME Autopilot_ssf
   12   #define S_FUNCTION_LEVEL 2
   13   #if !defined(RTW_GENERATED_S_FUNCTION)
   14   #define RTW_GENERATED_S_FUNCTION
   15   #endif
   16   
   17   #include <stdio.h>
   18   #include <string.h>
   19   #include "simstruc.h"
   20   #include "fixedpoint.h"
   21   
   22   #include "rtiostream_utils.h"
   23   
   24   static char * getSimulinkBlockPath(SimStruct *S) {
   25       char * simulinkBlockPath = NULL;
   26       const char * origBlockPath = ssGetPath(S);
   27       const char * searchString = "TmpSFcnForModelReference_";
   28       char * searchPtr;
   29       size_t origLength, searchAndNameLength, copyAmount;
   30       char * secondPart;
   31       size_t nameLength;
   32       origLength = strlen(origBlockPath);
   33       searchPtr = strstr(origBlockPath, searchString);
   34       if (searchPtr == NULL) {
   35           return simulinkBlockPath;
   36       }
   37       searchAndNameLength = strlen(searchPtr);
   38       copyAmount = origLength - searchAndNameLength;
   39       simulinkBlockPath = (char *) mxCalloc((mwSize) (origLength + 1), sizeof(char));
   40       simulinkBlockPath = strncpy(simulinkBlockPath, origBlockPath, copyAmount);
   41       simulinkBlockPath[copyAmount] = '\0';
   42       nameLength = searchAndNameLength - strlen(searchString);
   43       secondPart = &simulinkBlockPath[copyAmount];
   44       secondPart = strncpy(secondPart, &origBlockPath[origLength - nameLength], nameLength);
   45       secondPart[nameLength] = '\0';
   46       return simulinkBlockPath;
   47   }
   48   
   49   typedef struct {
   50       libH_type libH;
   51       char * lib;
   52       mxArray * MATLABObject;
   53       double streamID;
   54       double recvTimeout;
   55       double sendTimeout;
   56   } rtIOStreamData;
   57   
   58   typedef struct {
   59       uint8_T * data;
   60       mwSize size;
   61   } IOBuffer_T;
   62   
   63   typedef struct {
   64       FILE ** Fd;
   65       mwSize size;
   66       int32_T fidOffset;
   67   } targetIOFd_T;
   68   
   69   /* implements calls into MATLAB */
   70   static int callMATLAB(SimStruct * S, 
   71                         int nlhs, 
   72                         mxArray * plhs[], 
   73                         int nrhs, 
   74                         mxArray * prhs[], 
   75                         const char * functionName, 
   76                         int withTrap) {
   77       int errorOccurred = 0;
   78       if (withTrap) {
   79           mxArray * mException;
   80           mException = mexCallMATLABWithTrap(nlhs, plhs, nrhs, prhs, functionName);
   81           {
   82               int i;
   83               for (i=0; i<nrhs; i++) {
   84                   mxDestroyArray(prhs[i]);
   85               } /* for */
   86           }
   87           if (mException != NULL) {
   88               mxArray * rhsDisplayMException[1];
   89               errorOccurred = 1;
   90               rhsDisplayMException[0] = mException;
   91               mException = mexCallMATLABWithTrap(0, NULL, 1, rhsDisplayMException, "rtw.pil.SILPILInterface.displayMException");
   92               mxDestroyArray(rhsDisplayMException[0]);
   93               if (mException != NULL) {
   94                   mxDestroyArray(mException);
   95                   ssSetErrorStatus( S,"Error calling rtw.pil.SILPILInterface.displayMException on mException object.");
   96               } else {
   97                   ssSetErrorStatus( S,"mexCallMATLABWithTrap failed. See the full MException report in the MATLAB Command Window.");
   98               } /* if */
   99           } /* if */
  100       } else {
  101           errorOccurred = mexCallMATLAB(nlhs, plhs, nrhs, prhs, functionName);
  102           {
  103               int i;
  104               for (i=0; i<nrhs; i++) {
  105                   mxDestroyArray(prhs[i]);
  106               } /* for */
  107           }
  108           if (errorOccurred) {
  109               ssSetErrorStatus( S,"mexCallMATLAB failed!");
  110           } /* if */
  111       } /* if */
  112       return errorOccurred;
  113   }
  114   
  115   /* enlarges the dynamically allocated shared I/O buffer
  116    * IOBufferPtr->data is NULL on failure */
  117   static void growIOBuffer(SimStruct *S, IOBuffer_T * IOBuffer, mwSize requiredSize) {
  118       if (IOBuffer->size < requiredSize) {
  119           IOBuffer->data = mxRealloc(IOBuffer->data, requiredSize * sizeof(uint8_T));
  120           if (IOBuffer->data == NULL) {
  121               ssSetErrorStatus( S,"growIOBuffer: mxRealloc failed.");
  122           } else {
  123               mexMakeMemoryPersistent(IOBuffer->data);
  124               IOBuffer->size = requiredSize;
  125           } /* if */
  126       } /* if */
  127   }
  128   
  129   /* grow the buffer for target I/O Fd array
  130    * targetIOFd->Fd is NULL on failure */
  131   static void growTargetIOFd(SimStruct *S, targetIOFd_T * IOFd, mwSize requiredSize) {
  132       if (IOFd->size < requiredSize) {
  133           IOFd->Fd = mxRealloc(IOFd->Fd, requiredSize * sizeof(FILE*));
  134           if (IOFd->Fd == NULL) {
  135               ssSetErrorStatus( S,"growTargetIOFd: mxRealloc failed.");
  136           } else {
  137               mexMakeMemoryPersistent(IOFd->Fd);
  138               IOFd->size = requiredSize;
  139           } /* if */
  140       } /* if */
  141   }
  142   
  143   static void closeAndFreeTargetIOFd(SimStruct *S) {
  144       int i;
  145       if(ssGetPWork(S) != NULL) {
  146           targetIOFd_T * targetIOFdPtr = (targetIOFd_T *) ssGetPWorkValue(S, 2);
  147           if(targetIOFdPtr != NULL) {
  148               if (targetIOFdPtr->Fd != NULL) {
  149                   for(i=0; i<targetIOFdPtr->size; i++) {
  150                       if (targetIOFdPtr->Fd[i] != NULL) {
  151                           fclose(targetIOFdPtr->Fd[i]);
  152                       } /* if */
  153                   } /* for */
  154                   mxFree(targetIOFdPtr->Fd);
  155               } /* if */
  156               mxFree(targetIOFdPtr);
  157           } /* if */
  158           ssSetPWorkValue(S, 2, NULL);
  159       } /* if */
  160   }
  161   
  162   /* implements blocking receive via RtIOStream */
  163   static int blockingRtIOStreamRecv(SimStruct *S,  size_t recvSize, int withTrap, uint8_T * dataIn) {
  164   
  165       int errorOccurred = 0;
  166       int timeoutOccurred = 0;
  167       rtIOStreamData * rtIOStreamDataPtr = (rtIOStreamData *) ssGetPWorkValue(S, 0);
  168       int * pCommErrorOccurred = (int *) ssGetPWorkValue(S, 3);
  169   
  170       if (rtIOStreamDataPtr == NULL) {
  171           ssSetErrorStatus( S,"rtIOStreamDataPtr is NULL.");
  172           errorOccurred = 1;
  173           return errorOccurred;
  174       } /* if */
  175   
  176       if (pCommErrorOccurred == NULL) {
  177           ssSetErrorStatus( S,"pCommErrorOccurred is NULL.");
  178           errorOccurred = 1;
  179           return errorOccurred;
  180       } /* if */
  181   
  182       {
  183           int attemptToReceiveData = 1;
  184           while(attemptToReceiveData) {
  185               errorOccurred = 0;
  186               timeoutOccurred = 0;
  187               errorOccurred = rtIOStreamBlockingRecv(&rtIOStreamDataPtr->libH, 
  188                  (const int) rtIOStreamDataPtr->streamID, 
  189                  dataIn, 
  190                  recvSize, 
  191                  (const int) rtIOStreamDataPtr->recvTimeout, 
  192                  &timeoutOccurred);
  193   
  194               if (errorOccurred && timeoutOccurred) {
  195                   mxArray *rhs[3];
  196                   mxArray *lhs[1];
  197                   char * simulinkBlockPath = getSimulinkBlockPath(S);
  198                   if (simulinkBlockPath == NULL) {
  199                       ssSetErrorStatus(S, "ModelBlock SIL/PIL unexpected error: getSimulinkBlockPath returned NULL pointer. Check search string was found in ssGetPath.\n");
  200                       return errorOccurred;
  201                   }
  202                   rhs[ 0 ] = mxCreateString("@coder.connectivity.SimulinkInterface.getSILPILInterface");
  203                   rhs[ 1 ] = mxCreateDoubleScalar( 1 );
  204                   rhs[ 2 ] = mxCreateString(simulinkBlockPath);
  205                   callMATLAB(S,  1, lhs, 3, rhs, "rtw.pil.SILPILInterface.sfunctionIsSILDebugEnabledAndSILExeAlive", 0);
  206                   mxFree((void *) simulinkBlockPath);
  207                   attemptToReceiveData = (int) *mxGetPr(lhs[0]);
  208                   mxDestroyArray(lhs[0]);
  209               } else {
  210                   /* rtIOStreamBlockingRecv completed successfully or a non-timeout related error occurred */
  211                   attemptToReceiveData = 0;
  212               } /* if */
  213           } /* while */
  214       }
  215   
  216       *pCommErrorOccurred = 0;
  217       if (errorOccurred) {
  218           mxArray *rhs[2];
  219           rhs[0] = mxCreateDoubleScalar(timeoutOccurred);
  220           rhs[1] = mxCreateDoubleScalar(rtIOStreamDataPtr->recvTimeout);
  221           *pCommErrorOccurred = 1;
  222           errorOccurred = callMATLAB(S,  0, NULL, 2, rhs, "rtw.pil.SILPILInterface.handleRtIOStreamRecvError", withTrap);
  223       } /* if */
  224   
  225       return errorOccurred;
  226   }
  227   
  228   /* implements blocking send via RtIOStream */
  229   static int blockingRtIOStreamSend(SimStruct *S, uint8_T * dataOut, size_t sendSize, int withTrap) {
  230   
  231       int errorOccurred = 0;
  232       int timeoutOccurred = 0;
  233       rtIOStreamData * rtIOStreamDataPtr = (rtIOStreamData *) ssGetPWorkValue(S, 0);
  234       int * pCommErrorOccurred = (int *) ssGetPWorkValue(S, 3);
  235   
  236       if (rtIOStreamDataPtr == NULL) {
  237           ssSetErrorStatus( S,"rtIOStreamDataPtr is NULL.");
  238           errorOccurred = 1;
  239           return errorOccurred;
  240       } /* if */
  241   
  242       if (pCommErrorOccurred == NULL) {
  243           ssSetErrorStatus( S,"pCommErrorOccurred is NULL.");
  244           errorOccurred = 1;
  245           return errorOccurred;
  246       } /* if */
  247   
  248       {
  249           int attemptToSendData = 1;
  250           while(attemptToSendData) {
  251               errorOccurred = 0;
  252               timeoutOccurred = 0;
  253               errorOccurred = rtIOStreamBlockingSend(&rtIOStreamDataPtr->libH, 
  254                  (const int) rtIOStreamDataPtr->streamID, 
  255                  dataOut, 
  256                  sendSize, 
  257                  (const int) rtIOStreamDataPtr->sendTimeout, 
  258                  &timeoutOccurred);
  259   
  260               if (errorOccurred && timeoutOccurred) {
  261                   mxArray *rhs[3];
  262                   mxArray *lhs[1];
  263                   char * simulinkBlockPath = getSimulinkBlockPath(S);
  264                   if (simulinkBlockPath == NULL) {
  265                       ssSetErrorStatus(S, "ModelBlock SIL/PIL unexpected error: getSimulinkBlockPath returned NULL pointer. Check search string was found in ssGetPath.\n");
  266                       return errorOccurred;
  267                   }
  268                   rhs[ 0 ] = mxCreateString("@coder.connectivity.SimulinkInterface.getSILPILInterface");
  269                   rhs[ 1 ] = mxCreateDoubleScalar( 1 );
  270                   rhs[ 2 ] = mxCreateString(simulinkBlockPath);
  271                   callMATLAB(S,  1, lhs, 3, rhs, "rtw.pil.SILPILInterface.sfunctionIsSILDebugEnabledAndSILExeAlive", 0);
  272                   mxFree((void *) simulinkBlockPath);
  273                   attemptToSendData = (int) *mxGetPr(lhs[0]);
  274                   mxDestroyArray(lhs[0]);
  275               } else {
  276                   /* rtIOStreamBlockingSend completed successfully or a non-timeout related error occurred */
  277                   attemptToSendData = 0;
  278               } /* if */
  279           } /* while */
  280       }
  281   
  282       *pCommErrorOccurred = 0;
  283       if (errorOccurred) {
  284           mxArray *rhs[2];
  285           rhs[0] = mxCreateDoubleScalar(timeoutOccurred);
  286           rhs[1] = mxCreateDoubleScalar(rtIOStreamDataPtr->sendTimeout);
  287           *pCommErrorOccurred = 1;
  288           errorOccurred = callMATLAB(S,  0, NULL, 2, rhs, "rtw.pil.SILPILInterface.handleRtIOStreamSendError", withTrap);
  289       } /* if */
  290   
  291       return errorOccurred;
  292   }
  293   
  294   /* implements command dispatch layer */
  295   static int commandDispatch(SimStruct *S, IOBuffer_T * IOBuffer, mwSize dataOutSize, mwSize * dataInSize, int withTrap) {
  296       int errorOccurred = 0;
  297       uint8_T * mxMemUnitPtr;
  298       mwSize responseSize = 0;
  299       uint8_T responseHeaderData[5];
  300       uint8_T COMMAND_COMPLETE = 0;
  301       *dataInSize = 0;
  302       errorOccurred = blockingRtIOStreamSend(S, IOBuffer->data, dataOutSize, withTrap);
  303       if (errorOccurred) {
  304           return errorOccurred;
  305       } /* if */
  306       while (!COMMAND_COMPLETE) {
  307           uint32_T WRITE_DATA_AVAIL;
  308           errorOccurred = blockingRtIOStreamRecv(S, 5, withTrap, &responseHeaderData[0]);
  309           if (errorOccurred) {
  310               return errorOccurred;
  311           } /* if */
  312           mxMemUnitPtr = (uint8_T *) &responseHeaderData[0];
  313           {
  314               uint8_T * simDataMemUnitPtr;
  315               simDataMemUnitPtr = (uint8_T *) &WRITE_DATA_AVAIL;
  316               {
  317                   size_t num_elements = 1;
  318                   size_t bytes_consumed = num_elements * sizeof(uint32_T);
  319                   memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
  320                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
  321               }
  322           }
  323           {
  324               uint8_T * simDataMemUnitPtr;
  325               simDataMemUnitPtr = (uint8_T *) &COMMAND_COMPLETE;
  326               {
  327                   size_t num_elements = 1;
  328                   size_t bytes_consumed = num_elements * sizeof(uint8_T);
  329                   memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
  330                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
  331               }
  332           }
  333           if (WRITE_DATA_AVAIL) {
  334               growIOBuffer(S, IOBuffer, responseSize + (mwSize) WRITE_DATA_AVAIL);
  335               if (IOBuffer->data == NULL) {
  336                   ssSetErrorStatus( S,"commandDispatch: growIOBuffer failed.");
  337                   errorOccurred = 1;
  338                   return errorOccurred;
  339               } /* if */
  340               errorOccurred = blockingRtIOStreamRecv(S, WRITE_DATA_AVAIL, withTrap, &IOBuffer->data[responseSize]);
  341               if (errorOccurred) {
  342                   return errorOccurred;
  343               } /* if */
  344               responseSize += WRITE_DATA_AVAIL;
  345           } /* if */
  346       } /* while */
  347       *dataInSize = responseSize;
  348       return errorOccurred;
  349   }
  350   
  351   	MdlRefChildMdlRec childModels[8] = {
  352             "Altitude_Mode","Autopilot:pitch_ap:Altitude_Mode",0 , 
  353             "Autopilot","Autopilot",0 , 
  354             "Heading_Mode","Autopilot:roll_ap:Heading_Mode",0 , 
  355             "attitude_controller","Autopilot:pitch_ap:attitude_controller",0 , 
  356             "attitude_controller0","Autopilot:roll_ap:attitude_controller0",0 , 
  357             "pitch_ap","Autopilot:pitch_ap",0 , 
  358             "roll_ap","Autopilot:roll_ap",0 , 
  359             "yaw_damper","Autopilot:yaw_damper",0 };
  360   
  361   
  362   /* This function checks the attributes of tunable parameters. */
  363   #define MDL_CHECK_PARAMETERS
  364   #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
  365   static void mdlCheckParameters(SimStruct *S) {
  366   }
  367   #endif /* MDL_CHECK_PARAMETERS */
  368   
  369   static void mdlInitializeSizes(SimStruct *S) {
  370       ssSetNumSFcnParams(S, 0);  /* Number of expected parameters */
  371       if (ssGetNumSFcnParams(S) == ssGetSFcnParamsCount(S)) {
  372       #if defined(MDL_CHECK_PARAMETERS)
  373          mdlCheckParameters(S);
  374       #endif
  375          if (ssGetErrorStatus(S) != (NULL)) return;
  376       } else {
  377          /* Parameter mismatch will be reported by Simulink */
  378          return;
  379       }
  380       
  381       ssSetNumContStates(S, 0);
  382       ssSetNumDiscStates(S, 0);
  383       /* no support for SimState */
  384       ssSetSimStateCompliance(S, DISALLOW_SIM_STATE);
  385       
  386       /* Allow signal dimensions greater than 2 */
  387       ssAllowSignalsWithMoreThan2D(S);
  388       
  389       /* Allow fixed-point data types with 33 or more bits */
  390       ssFxpSetU32BitRegionCompliant(S,1);
  391       
  392       {
  393           mxArray * lhs[1];
  394           mxArray * error = NULL;
  395           char * installVersion;
  396           error = mexCallMATLABWithTrap(1, lhs, 0, NULL, "rtw.pil.SILPILInterface.getPILVersion");
  397           if (error != NULL) {
  398               mxDestroyArray(error);
  399               ssSetErrorStatus( S,"Failed to determine the installed In-the-Loop version for comparison against the In-the-Loop s-function version (release 6.5 (R2013b)_11). To avoid this error, remove the In-the-Loop s-function from your MATLAB path (e.g. delete it or move to a clean working directory).");
  400               return;
  401           } /* if */
  402           if (mxIsEmpty(lhs[0])) {
  403               ssSetErrorStatus( S,"rtw.pil.SILPILInterface.getPILVersion returned empty!");
  404               return;
  405           } /* if */
  406           installVersion = mxArrayToString(lhs[0]);
  407           mxDestroyArray(lhs[0]);
  408           if (installVersion == NULL) {
  409               ssSetErrorStatus( S,"Failed to determine installed In-the-Loop version.");
  410               return;
  411           } /* if */
  412           if (strcmp(installVersion, "6.5 (R2013b)_11") != 0) {
  413               ssSetErrorStatus( S,"The In-the-Loop s-function is incompatible with the installed In-the-Loop version (see ver('ecoder')); it was generated for release 6.5 (R2013b)_11. To avoid this error, remove the In-the-Loop s-function from your MATLAB path (e.g. delete it or move to a clean working directory)");
  414               return;
  415           } /* if */
  416           mxFree(installVersion);
  417       }
  418   
  419       if (S->mdlInfo->genericFcn != NULL) {
  420           _GenericFcn fcn = S->mdlInfo->genericFcn;
  421           int_T hwSettings[15];
  422           int_T  opSettings[1];
  423           boolean_T hasDiscTs;
  424           real_T lifeSpan, startTime,  stopTime;
  425           real_T rtInf;
  426   
  427           union {
  428               struct {
  429                   uint32_T wordL;
  430                   uint32_T wordH;
  431               } bitVal;
  432               real_T fltVal;
  433           } tmpVal;
  434   
  435           tmpVal.bitVal.wordH = 0x7FF00000U;
  436           tmpVal.bitVal.wordL = 0x00000000U;
  437           rtInf = tmpVal.fltVal;
  438   
  439           hasDiscTs = 1;
  440           lifeSpan = rtInf;
  441           startTime = 0.0;
  442           stopTime = 25.0;
  443           (fcn)(S, GEN_FCN_CHK_MODELREF_SOLVER_TYPE_EARLY, 2, NULL);
  444           if (!(fcn)(S, GEN_FCN_CHK_MODELREF_LIFE_SPAN, -1, &lifeSpan)) return;
  445           if (!(fcn)(S, GEN_FCN_CHK_MODELREF_START_TIME, -1, &startTime)) return;
  446           if (!(fcn)(S, GEN_FCN_CHK_MODELREF_STOP_TIME, -1, &stopTime)) return;
  447           hwSettings[0] = 8;
  448           hwSettings[1] = 16;
  449           hwSettings[2] = 32;
  450           hwSettings[3] = 32;
  451           hwSettings[4] = 32;
  452           hwSettings[5] = 64;
  453           hwSettings[6] = 32;
  454           hwSettings[7] = 0;
  455           hwSettings[8] = 2;
  456           hwSettings[9] = 32;
  457           hwSettings[10] = 1;
  458           hwSettings[11] = 0;
  459           hwSettings[12] = 2;
  460           hwSettings[13] = 64;
  461           hwSettings[14] = 0;
  462           if (!(fcn)(S, GEN_FCN_CHK_MODELREF_HARDWARE_SETTINGS, 15, hwSettings)) return;
  463           opSettings[0] = 0;
  464           if (!(fcn)(S, GEN_FCN_CHK_MODELREF_OPTIM_SETTINGS, 1, opSettings)) return;
  465           if (!(fcn)(S, GEN_FCN_CHK_MODELREF_CONCURRETNT_TASK_SUPPORT, 0, NULL)) return;
  466           if (!(fcn)(S, GEN_FCN_CHK_MODELREF_SOLVER_TYPE, 2, &hasDiscTs)) return;
  467           if (!(fcn)(S, GEN_FCN_CHK_MODELREF_SOLVER_MODE, SOLVER_MODE_SINGLETASKING, NULL)) return;
  468           if (!(fcn)(S, GEN_FCN_CHK_MODELREF_DATA_DICTIONARY, 0, (void *)"")) return;
  469       }
  470   
  471       ssSetRTWGeneratedSFcn(S, 2);
  472   
  473       if (S->mdlInfo->genericFcn != NULL) {
  474           _GenericFcn fcn = S->mdlInfo->genericFcn;
  475           (fcn)(S, GEN_FCN_CHK_MODELREF_FRAME_UPGRADE_DIAGNOSTICS, 1, NULL);
  476       }
  477   
  478   
  479       if (!ssSetNumInputPorts(S, 9)) return;
  480   
  481       /* Input Port 0 */
  482       /* contiguous inport */
  483       ssSetInputPortRequiredContiguous(S, 0, 1);
  484       /* directfeedthrough */
  485       ssSetInputPortDirectFeedThrough(S, 0, 1);
  486       if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
  487           DTypeId dataTypeId = INVALID_DTYPE_ID;
  488           ssRegisterTypeFromNamedObject(S, "slBus1", &dataTypeId);
  489           if (dataTypeId == INVALID_DTYPE_ID) return;
  490           ssSetInputPortDataType(S, 0, dataTypeId);
  491       }
  492       ssSetBusInputAsStruct(S, 0, 1);
  493       /* dimensions */
  494       {
  495           DECL_AND_INIT_DIMSINFO(di);
  496           int_T dims[ 1 ] = { 1 };
  497           di.numDims = 1;
  498           di.dims = dims;
  499           di.width = 1;
  500           ssSetInputPortDimensionInfo(S, 0, &di);
  501       }
  502       ssSetInputPortDimensionsMode(S, 0, FIXED_DIMS_MODE);
  503       /* complexity */
  504       ssSetInputPortComplexSignal(S, 0, COMPLEX_NO);
  505       /* using port based sample times */
  506       ssSetInputPortSampleTime(S, 0, 0.025);
  507       ssSetInputPortOffsetTime(S, 0, 0);
  508       /* sampling mode */
  509       ssSetInputPortFrameData(S, 0, FRAME_NO);
  510   
  511       /* Input Port 1 */
  512       /* contiguous inport */
  513       ssSetInputPortRequiredContiguous(S, 1, 1);
  514       /* directfeedthrough */
  515       ssSetInputPortDirectFeedThrough(S, 1, 1);
  516       if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
  517           DTypeId dataTypeId = INVALID_DTYPE_ID;
  518           ssRegisterTypeFromNamedObject(S, "slBus2", &dataTypeId);
  519           if (dataTypeId == INVALID_DTYPE_ID) return;
  520           ssSetInputPortDataType(S, 1, dataTypeId);
  521       }
  522       ssSetBusInputAsStruct(S, 1, 1);
  523       /* dimensions */
  524       {
  525           DECL_AND_INIT_DIMSINFO(di);
  526           int_T dims[ 1 ] = { 1 };
  527           di.numDims = 1;
  528           di.dims = dims;
  529           di.width = 1;
  530           ssSetInputPortDimensionInfo(S, 1, &di);
  531       }
  532       ssSetInputPortDimensionsMode(S, 1, FIXED_DIMS_MODE);
  533       /* complexity */
  534       ssSetInputPortComplexSignal(S, 1, COMPLEX_NO);
  535       /* using port based sample times */
  536       ssSetInputPortSampleTime(S, 1, 0.025);
  537       ssSetInputPortOffsetTime(S, 1, 0);
  538       /* sampling mode */
  539       ssSetInputPortFrameData(S, 1, FRAME_NO);
  540   
  541       /* Input Port 2 */
  542       /* contiguous inport */
  543       ssSetInputPortRequiredContiguous(S, 2, 1);
  544       /* directfeedthrough */
  545       ssSetInputPortDirectFeedThrough(S, 2, 1);
  546       if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
  547           DTypeId dataTypeId = INVALID_DTYPE_ID;
  548           /* set datatype */
  549           dataTypeId = 8;
  550           ssSetInputPortDataType(S, 2, dataTypeId);
  551       }
  552       /* dimensions */
  553       {
  554           DECL_AND_INIT_DIMSINFO(di);
  555           int_T dims[ 1 ] = { 1 };
  556           di.numDims = 1;
  557           di.dims = dims;
  558           di.width = 1;
  559           ssSetInputPortDimensionInfo(S, 2, &di);
  560       }
  561       ssSetInputPortDimensionsMode(S, 2, FIXED_DIMS_MODE);
  562       /* complexity */
  563       ssSetInputPortComplexSignal(S, 2, COMPLEX_NO);
  564       /* using port based sample times */
  565       ssSetInputPortSampleTime(S, 2, 0.025);
  566       ssSetInputPortOffsetTime(S, 2, 0);
  567       /* sampling mode */
  568       ssSetInputPortFrameData(S, 2, FRAME_NO);
  569   
  570       /* Input Port 3 */
  571       /* contiguous inport */
  572       ssSetInputPortRequiredContiguous(S, 3, 1);
  573       /* directfeedthrough */
  574       ssSetInputPortDirectFeedThrough(S, 3, 1);
  575       if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
  576           DTypeId dataTypeId = INVALID_DTYPE_ID;
  577           /* set datatype */
  578           dataTypeId = 8;
  579           ssSetInputPortDataType(S, 3, dataTypeId);
  580       }
  581       /* dimensions */
  582       {
  583           DECL_AND_INIT_DIMSINFO(di);
  584           int_T dims[ 1 ] = { 1 };
  585           di.numDims = 1;
  586           di.dims = dims;
  587           di.width = 1;
  588           ssSetInputPortDimensionInfo(S, 3, &di);
  589       }
  590       ssSetInputPortDimensionsMode(S, 3, FIXED_DIMS_MODE);
  591       /* complexity */
  592       ssSetInputPortComplexSignal(S, 3, COMPLEX_NO);
  593       /* using port based sample times */
  594       ssSetInputPortSampleTime(S, 3, 0.025);
  595       ssSetInputPortOffsetTime(S, 3, 0);
  596       /* sampling mode */
  597       ssSetInputPortFrameData(S, 3, FRAME_NO);
  598   
  599       /* Input Port 4 */
  600       /* contiguous inport */
  601       ssSetInputPortRequiredContiguous(S, 4, 1);
  602       /* directfeedthrough */
  603       ssSetInputPortDirectFeedThrough(S, 4, 1);
  604       if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
  605           DTypeId dataTypeId = INVALID_DTYPE_ID;
  606           /* set datatype */
  607           dataTypeId = 8;
  608           ssSetInputPortDataType(S, 4, dataTypeId);
  609       }
  610       /* dimensions */
  611       {
  612           DECL_AND_INIT_DIMSINFO(di);
  613           int_T dims[ 1 ] = { 1 };
  614           di.numDims = 1;
  615           di.dims = dims;
  616           di.width = 1;
  617           ssSetInputPortDimensionInfo(S, 4, &di);
  618       }
  619       ssSetInputPortDimensionsMode(S, 4, FIXED_DIMS_MODE);
  620       /* complexity */
  621       ssSetInputPortComplexSignal(S, 4, COMPLEX_NO);
  622       /* using port based sample times */
  623       ssSetInputPortSampleTime(S, 4, 0.025);
  624       ssSetInputPortOffsetTime(S, 4, 0);
  625       /* sampling mode */
  626       ssSetInputPortFrameData(S, 4, FRAME_NO);
  627   
  628       /* Input Port 5 */
  629       /* contiguous inport */
  630       ssSetInputPortRequiredContiguous(S, 5, 1);
  631       /* directfeedthrough */
  632       ssSetInputPortDirectFeedThrough(S, 5, 1);
  633       if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
  634           DTypeId dataTypeId = INVALID_DTYPE_ID;
  635           /* set datatype */
  636           dataTypeId = 0;
  637           ssSetInputPortDataType(S, 5, dataTypeId);
  638       }
  639       /* dimensions */
  640       {
  641           DECL_AND_INIT_DIMSINFO(di);
  642           int_T dims[ 1 ] = { 1 };
  643           di.numDims = 1;
  644           di.dims = dims;
  645           di.width = 1;
  646           ssSetInputPortDimensionInfo(S, 5, &di);
  647       }
  648       ssSetInputPortDimensionsMode(S, 5, FIXED_DIMS_MODE);
  649       /* complexity */
  650       ssSetInputPortComplexSignal(S, 5, COMPLEX_NO);
  651       /* using port based sample times */
  652       ssSetInputPortSampleTime(S, 5, 0.025);
  653       ssSetInputPortOffsetTime(S, 5, 0);
  654       /* sampling mode */
  655       ssSetInputPortFrameData(S, 5, FRAME_NO);
  656   
  657       /* Input Port 6 */
  658       /* contiguous inport */
  659       ssSetInputPortRequiredContiguous(S, 6, 1);
  660       /* directfeedthrough */
  661       ssSetInputPortDirectFeedThrough(S, 6, 1);
  662       if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
  663           DTypeId dataTypeId = INVALID_DTYPE_ID;
  664           /* set datatype */
  665           dataTypeId = 0;
  666           ssSetInputPortDataType(S, 6, dataTypeId);
  667       }
  668       /* dimensions */
  669       {
  670           DECL_AND_INIT_DIMSINFO(di);
  671           int_T dims[ 1 ] = { 1 };
  672           di.numDims = 1;
  673           di.dims = dims;
  674           di.width = 1;
  675           ssSetInputPortDimensionInfo(S, 6, &di);
  676       }
  677       ssSetInputPortDimensionsMode(S, 6, FIXED_DIMS_MODE);
  678       /* complexity */
  679       ssSetInputPortComplexSignal(S, 6, COMPLEX_NO);
  680       /* using port based sample times */
  681       ssSetInputPortSampleTime(S, 6, 0.025);
  682       ssSetInputPortOffsetTime(S, 6, 0);
  683       /* sampling mode */
  684       ssSetInputPortFrameData(S, 6, FRAME_NO);
  685   
  686       /* Input Port 7 */
  687       /* contiguous inport */
  688       ssSetInputPortRequiredContiguous(S, 7, 1);
  689       /* directfeedthrough */
  690       ssSetInputPortDirectFeedThrough(S, 7, 1);
  691       if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
  692           DTypeId dataTypeId = INVALID_DTYPE_ID;
  693           /* set datatype */
  694           dataTypeId = 0;
  695           ssSetInputPortDataType(S, 7, dataTypeId);
  696       }
  697       /* dimensions */
  698       {
  699           DECL_AND_INIT_DIMSINFO(di);
  700           int_T dims[ 1 ] = { 1 };
  701           di.numDims = 1;
  702           di.dims = dims;
  703           di.width = 1;
  704           ssSetInputPortDimensionInfo(S, 7, &di);
  705       }
  706       ssSetInputPortDimensionsMode(S, 7, FIXED_DIMS_MODE);
  707       /* complexity */
  708       ssSetInputPortComplexSignal(S, 7, COMPLEX_NO);
  709       /* using port based sample times */
  710       ssSetInputPortSampleTime(S, 7, 0.025);
  711       ssSetInputPortOffsetTime(S, 7, 0);
  712       /* sampling mode */
  713       ssSetInputPortFrameData(S, 7, FRAME_NO);
  714   
  715       /* Input Port 8 */
  716       /* contiguous inport */
  717       ssSetInputPortRequiredContiguous(S, 8, 1);
  718       /* directfeedthrough */
  719       ssSetInputPortDirectFeedThrough(S, 8, 1);
  720       if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
  721           DTypeId dataTypeId = INVALID_DTYPE_ID;
  722           /* set datatype */
  723           dataTypeId = 0;
  724           ssSetInputPortDataType(S, 8, dataTypeId);
  725       }
  726       /* dimensions */
  727       {
  728           DECL_AND_INIT_DIMSINFO(di);
  729           int_T dims[ 1 ] = { 1 };
  730           di.numDims = 1;
  731           di.dims = dims;
  732           di.width = 1;
  733           ssSetInputPortDimensionInfo(S, 8, &di);
  734       }
  735       ssSetInputPortDimensionsMode(S, 8, FIXED_DIMS_MODE);
  736       /* complexity */
  737       ssSetInputPortComplexSignal(S, 8, COMPLEX_NO);
  738       /* using port based sample times */
  739       ssSetInputPortSampleTime(S, 8, 0.025);
  740       ssSetInputPortOffsetTime(S, 8, 0);
  741       /* sampling mode */
  742       ssSetInputPortFrameData(S, 8, FRAME_NO);
  743   
  744       if (!ssSetNumOutputPorts(S, 3)) return;
  745   
  746       /* Output Port 0 */
  747       if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
  748           DTypeId dataTypeId = INVALID_DTYPE_ID;
  749           /* set datatype */
  750           dataTypeId = 0;
  751           ssSetOutputPortDataType(S, 0, dataTypeId);
  752       }
  753       /* dimensions */
  754       {
  755           DECL_AND_INIT_DIMSINFO(di);
  756           int_T dims[ 1 ] = { 1 };
  757           di.numDims = 1;
  758           di.dims = dims;
  759           di.width = 1;
  760           ssSetOutputPortDimensionInfo(S, 0, &di);
  761       }
  762       ssSetOutputPortDimensionsMode(S, 0, FIXED_DIMS_MODE);
  763       /* complexity */
  764       ssSetOutputPortComplexSignal(S, 0, COMPLEX_NO);
  765       /* using port based sample times */
  766       ssSetOutputPortSampleTime(S, 0, 0.025);
  767       ssSetOutputPortOffsetTime(S, 0, 0);
  768       /* sampling mode */
  769       ssSetOutputPortFrameData(S, 0, FRAME_NO);
  770   
  771       /* Output Port 1 */
  772       if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
  773           DTypeId dataTypeId = INVALID_DTYPE_ID;
  774           /* set datatype */
  775           dataTypeId = 0;
  776           ssSetOutputPortDataType(S, 1, dataTypeId);
  777       }
  778       /* dimensions */
  779       {
  780           DECL_AND_INIT_DIMSINFO(di);
  781           int_T dims[ 1 ] = { 1 };
  782           di.numDims = 1;
  783           di.dims = dims;
  784           di.width = 1;
  785           ssSetOutputPortDimensionInfo(S, 1, &di);
  786       }
  787       ssSetOutputPortDimensionsMode(S, 1, FIXED_DIMS_MODE);
  788       /* complexity */
  789       ssSetOutputPortComplexSignal(S, 1, COMPLEX_NO);
  790       /* using port based sample times */
  791       ssSetOutputPortSampleTime(S, 1, 0.025);
  792       ssSetOutputPortOffsetTime(S, 1, 0);
  793       /* sampling mode */
  794       ssSetOutputPortFrameData(S, 1, FRAME_NO);
  795   
  796       /* Output Port 2 */
  797       if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
  798           DTypeId dataTypeId = INVALID_DTYPE_ID;
  799           /* set datatype */
  800           dataTypeId = 0;
  801           ssSetOutputPortDataType(S, 2, dataTypeId);
  802       }
  803       /* dimensions */
  804       {
  805           DECL_AND_INIT_DIMSINFO(di);
  806           int_T dims[ 1 ] = { 1 };
  807           di.numDims = 1;
  808           di.dims = dims;
  809           di.width = 1;
  810           ssSetOutputPortDimensionInfo(S, 2, &di);
  811       }
  812       ssSetOutputPortDimensionsMode(S, 2, FIXED_DIMS_MODE);
  813       /* complexity */
  814       ssSetOutputPortComplexSignal(S, 2, COMPLEX_NO);
  815       /* using port based sample times */
  816       ssSetOutputPortSampleTime(S, 2, 0.025);
  817       ssSetOutputPortOffsetTime(S, 2, 0);
  818       /* sampling mode */
  819       ssSetOutputPortFrameData(S, 2, FRAME_NO);
  820   
  821       /* using port based sample times */
  822       ssSetNumSampleTimes(S, PORT_BASED_SAMPLE_TIMES);
  823       ssSetNumPWork(S, 4);
  824       ssSetNumRWork(S, 0);
  825       ssSetNumIWork(S, 0);
  826       ssSetNumModes(S, 0);
  827       ssSetNumNonsampledZCs(S, 0);
  828       /* this s-function is sample time dependent: do not allow sub-models containing it to inherit sample times */
  829       ssSetModelReferenceSampleTimeInheritanceRule(S, DISALLOW_SAMPLE_TIME_INHERITANCE);
  830       {
  831           uint_T options = 0;
  832           /* do not allow (including inheritance of) constant block-based sample times*/
  833           options |= SS_OPTION_DISALLOW_CONSTANT_SAMPLE_TIME;
  834           options |= SS_OPTION_SUPPORTS_ALIAS_DATA_TYPES;
  835           options |= SS_OPTION_CALL_TERMINATE_ON_EXIT;
  836           ssSetOptions(S, options);
  837       }
  838       ssSetModelReferenceNormalModeSupport(S, MDL_START_AND_MDL_PROCESS_PARAMS_OK);
  839       slmrSetHasNonVirtualConstantTs(S, true);
  840       {
  841           static ssRTWStorageType storageClass[12] = {SS_RTW_STORAGE_AUTO, SS_RTW_STORAGE_AUTO, SS_RTW_STORAGE_AUTO, SS_RTW_STORAGE_AUTO, SS_RTW_STORAGE_AUTO, SS_RTW_STORAGE_AUTO, SS_RTW_STORAGE_AUTO, SS_RTW_STORAGE_AUTO, SS_RTW_STORAGE_AUTO, SS_RTW_STORAGE_AUTO, SS_RTW_STORAGE_AUTO, SS_RTW_STORAGE_AUTO};
  842           ssSetModelRefPortRTWStorageClasses(S, storageClass);
  843       }
  844   
  845       /* Set output port IC attributes */
  846       ssSetOutputPortICAttributes(S, 0, false, false, false);
  847       /* Set output port IC attributes */
  848       ssSetOutputPortICAttributes(S, 1, false, false, false);
  849       /* Set output port IC attributes */
  850       ssSetOutputPortICAttributes(S, 2, false, false, false);
  851       slmrModelRefSetOutputPortDrivenByNonCondExecStateflow(S, 0, false);
  852       slmrModelRefSetOutputPortDrivenByNonCondExecStateflow(S, 1, false);
  853       slmrModelRefSetOutputPortDrivenByNonCondExecStateflow(S, 2, false);
  854       ssSetModelRefOutputBlkIOUpdatedInAnotherExecContext(S, 0, false);
  855       ssSetModelRefOutputBlkIOUpdatedInAnotherExecContext(S, 1, false);
  856       ssSetModelRefOutputBlkIOUpdatedInAnotherExecContext(S, 2, false);
  857       ssSetModelReferenceConsistentOutportInitialization(S, true);
  858       ssSetModelRefHasEnablePort(S,0);
  859       ssSetOptimizeModelRefInitCode(S, 1);
  860       if (S->mdlInfo->genericFcn != (NULL)) {
  861           	ssRegModelRefChildModel(S, 8, childModels);
  862   
  863       }
  864       ssSetModelRefSignalLoggingSaveFormat(S, SS_LOG_FORMAT_MIXED);
  865   }
  866   
  867   #define MDL_SET_INPUT_PORT_SAMPLE_TIME /* Change to #undef to remove function */
  868   #if defined(MDL_SET_INPUT_PORT_SAMPLE_TIME) && defined(MATLAB_MEX_FILE)
  869   static void mdlSetInputPortSampleTime(SimStruct *S, int_T portIdx, real_T sampleTime, real_T offsetTime) {
  870       /* sample times are fully specified, not inherited */
  871   }
  872   #endif /* MDL_SET_INPUT_PORT_SAMPLE_TIME */
  873   
  874   #define MDL_SET_OUTPUT_PORT_SAMPLE_TIME /* Change to #undef to remove function */
  875   #if defined(MDL_SET_OUTPUT_PORT_SAMPLE_TIME) && defined(MATLAB_MEX_FILE)
  876   static void mdlSetOutputPortSampleTime(SimStruct *S, int_T portIdx, real_T sampleTime, real_T offsetTime) {
  877       /* sample times are fully specified, not inherited */
  878   }
  879   #endif /* MDL_SET_OUTPUT_PORT_SAMPLE_TIME */
  880   
  881   static void mdlInitializeSampleTimes(SimStruct *S) {
  882       /* using port based sample times */
  883   }
  884   
  885   /* forward declaration for mdlProcessParameters */
  886   #define MDL_PROCESS_PARAMETERS  /* Change to #undef to remove function */
  887   #if defined(MDL_PROCESS_PARAMETERS)
  888   static void mdlProcessParameters(SimStruct *S);
  889   #endif /* MDL_PROCESS_PARAMETERS */
  890   #define MDL_START  /* Change to #undef to remove function */
  891   #if defined(MDL_START)
  892   static void mdlStart(SimStruct *S) {
  893       {
  894           mxArray *rhs[4];
  895           mxArray *lhs[1];
  896           char * rootLoggingPath;
  897           char * simulinkBlockPath = getSimulinkBlockPath(S);
  898           if (simulinkBlockPath == NULL) {
  899               ssSetErrorStatus(S, "ModelBlock SIL/PIL unexpected error: getSimulinkBlockPath returned NULL pointer. Check search string was found in ssGetPath.\n");
  900               return;
  901           }
  902           rhs[ 0 ] = mxCreateString("@coder.connectivity.SimulinkInterface.getSILPILInterface");
  903           rhs[ 1 ] = mxCreateDoubleScalar( 1 );
  904           rhs[ 2 ] = mxCreateString(simulinkBlockPath);
  905           rhs[3] = mxCreateString(ssGetPath(ssGetRootSS(S)));
  906           callMATLAB(S,  1, lhs, 4 , rhs, "rtw.pil.SILPILInterface.sfunctionInitializeHook", 0);
  907           rootLoggingPath = mxArrayToString(lhs[0]);
  908           mxDestroyArray(lhs[0]);
  909           {
  910               int * pCommErrorOccurred = (int *) mxCalloc(1, sizeof(int));
  911               if (pCommErrorOccurred == NULL) {
  912                   ssSetErrorStatus( S,"Error in allocating memory for pCommErrorOccurred through mxCalloc.");
  913                   return;
  914               } /* if */
  915               *pCommErrorOccurred = 0;
  916               mexMakeMemoryPersistent(pCommErrorOccurred);
  917               ssSetPWorkValue(S, 3, pCommErrorOccurred);
  918           }
  919   
  920           mxFree((void *) simulinkBlockPath);
  921           mxFree(rootLoggingPath);
  922       }
  923       {
  924           {
  925               mxArray *rhs[4];
  926               char * simulinkBlockPath = getSimulinkBlockPath(S);
  927               if (simulinkBlockPath == NULL) {
  928                   ssSetErrorStatus(S, "ModelBlock SIL/PIL unexpected error: getSimulinkBlockPath returned NULL pointer. Check search string was found in ssGetPath.\n");
  929                   return;
  930               }
  931               rhs[ 0 ] = mxCreateString("@coder.connectivity.SimulinkInterface.getSILPILInterface");
  932               rhs[ 1 ] = mxCreateDoubleScalar( 1 );
  933               rhs[ 2 ] = mxCreateString(simulinkBlockPath);
  934               rhs[3] = mxCreateString("uint8");
  935               callMATLAB(S,  0, NULL, 4, rhs, "rtw.pil.SILPILInterface.sfunctionPILStartHook", 0);
  936               mxFree((void *) simulinkBlockPath);
  937           }
  938       }
  939       {
  940           mxArray *rhs[3];
  941           mxArray *lhs[5];
  942           char * simulinkBlockPath = getSimulinkBlockPath(S);
  943           if (simulinkBlockPath == NULL) {
  944               ssSetErrorStatus(S, "ModelBlock SIL/PIL unexpected error: getSimulinkBlockPath returned NULL pointer. Check search string was found in ssGetPath.\n");
  945               return;
  946           }
  947           rhs[ 0 ] = mxCreateString("@coder.connectivity.SimulinkInterface.getSILPILInterface");
  948           rhs[ 1 ] = mxCreateDoubleScalar( 1 );
  949           rhs[ 2 ] = mxCreateString(simulinkBlockPath);
  950           callMATLAB(S,  5, lhs, 3, rhs, "rtw.pil.SILPILInterface.sfunctionGetRtIOStreamInfoHook", 0);
  951           mxFree((void *) simulinkBlockPath);
  952           {
  953               rtIOStreamData * rtIOStreamDataPtr = (rtIOStreamData *) mxCalloc(1, sizeof(rtIOStreamData));
  954               if (rtIOStreamDataPtr == NULL) {
  955                   ssSetErrorStatus( S,"Error in allocating memory through mxCalloc.");
  956                   return;
  957               } /* if */
  958               rtIOStreamDataPtr->lib = mxArrayToString(lhs[0]);
  959               rtIOStreamDataPtr->MATLABObject = mxDuplicateArray(lhs[1]);
  960               mexMakeMemoryPersistent(rtIOStreamDataPtr);
  961               mexMakeMemoryPersistent(rtIOStreamDataPtr->lib);
  962               mexMakeArrayPersistent(rtIOStreamDataPtr->MATLABObject);
  963               rtIOStreamDataPtr->streamID = *mxGetPr(lhs[2]);
  964               rtIOStreamDataPtr->recvTimeout = *mxGetPr(lhs[3]);
  965               rtIOStreamDataPtr->sendTimeout = *mxGetPr(lhs[4]);
  966               {
  967                   int errorStatus = rtIOStreamLoadLib(&rtIOStreamDataPtr->libH, rtIOStreamDataPtr->lib);
  968                   if (errorStatus) {
  969                       ssSetErrorStatus( S,"rtIOStreamLoadLib failed.");
  970                       return;
  971                   } /* if */
  972               }
  973               ssSetPWorkValue(S, 0, rtIOStreamDataPtr);
  974           }
  975           {
  976               int i;
  977               for (i=0; i<5; i++) {
  978                   mxDestroyArray(lhs[i]);
  979               } /* for */
  980           }
  981       }
  982       {
  983           IOBuffer_T * IOBufferPtr = (IOBuffer_T *) mxCalloc(1, sizeof(IOBuffer_T));
  984           if (IOBufferPtr == NULL) {
  985               ssSetErrorStatus( S,"Error in allocating memory through mxCalloc.");
  986               return;
  987           } /* if */
  988           mexMakeMemoryPersistent(IOBufferPtr);
  989           ssSetPWorkValue(S, 1, IOBufferPtr);
  990       }
  991       {
  992           targetIOFd_T * targetIOFdPtr = (targetIOFd_T *) mxCalloc(1, sizeof(targetIOFd_T));
  993           if (targetIOFdPtr == NULL) {
  994               return;
  995           } /* if */
  996           mexMakeMemoryPersistent(targetIOFdPtr);
  997           targetIOFdPtr->size = 0;
  998           targetIOFdPtr->Fd = NULL;
  999           targetIOFdPtr->fidOffset = -1;
 1000           ssSetPWorkValue(S, 2, targetIOFdPtr);
 1001       }
 1002       {
 1003           uint8_T * mxMemUnitPtr;
 1004           mwSize dataInSize = 0;
 1005           IOBuffer_T * IOBufferPtr = (IOBuffer_T *) ssGetPWorkValue(S, 1);
 1006           if (IOBufferPtr != NULL) {
 1007               growIOBuffer(S, IOBufferPtr, 5);
 1008               if (IOBufferPtr->data != NULL) {
 1009                   mxMemUnitPtr = (uint8_T *) IOBufferPtr->data;
 1010                   /* write command id */
 1011                   *mxMemUnitPtr = 0;
 1012                   mxMemUnitPtr++;
 1013                   {
 1014                       uint8_T * simDataMemUnitPtr;
 1015                       uint32_T commandDataFcnid = 0;
 1016                       simDataMemUnitPtr = (uint8_T *) &commandDataFcnid;
 1017                       {
 1018                           size_t num_elements = 1;
 1019                           size_t bytes_consumed = num_elements * sizeof(uint32_T);
 1020                           memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 1021                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1022                       }
 1023                   }
 1024                   {
 1025                       int errorOccurred;
 1026                       errorOccurred = commandDispatch(S, IOBufferPtr, 5, &dataInSize, 0);
 1027                       if (errorOccurred) {
 1028                           return;
 1029                       } /* if */
 1030                   }
 1031                   if (dataInSize > 0) {
 1032                       size_t dataInMemUnitSize = dataInSize;
 1033                       uint8_T responseId;
 1034                       uint8_T * mxMemUnitPtrEnd;
 1035                       #define RESPONSE_ERROR 0
 1036                       #define RESPONSE_PRINTF 3
 1037                       #define RESPONSE_FOPEN 4
 1038                       #define RESPONSE_FPRINTF 5
 1039                       #define TARGET_IO_SUCCESS 0
 1040                       #define RESPONSE_TYPE_SIZE 6
 1041                       mxMemUnitPtr = (uint8_T *) IOBufferPtr->data;
 1042                       mxMemUnitPtrEnd = mxMemUnitPtr + dataInMemUnitSize - 1;
 1043                       while (mxMemUnitPtr <= mxMemUnitPtrEnd) {
 1044                           /* read response id */
 1045                           responseId = *mxMemUnitPtr;
 1046                           mxMemUnitPtr++;
 1047                           switch (responseId) {
 1048                               case RESPONSE_ERROR: {
 1049                                   uint8_T errorId = *mxMemUnitPtr;
 1050                                   mxMemUnitPtr++;
 1051                                   if (errorId) {
 1052                                       {
 1053                                           mxArray * prhs[ 2 ];
 1054                                           prhs[0] = mxCreateString("PIL:pilverification:PILError");
 1055                                           prhs[1] = mxCreateDoubleScalar(errorId);
 1056                                           callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1057                                           return;
 1058                                       }
 1059                                   } /* if */
 1060                                   break;
 1061                               }
 1062                               case RESPONSE_PRINTF: {
 1063                                   uint8_T PRINTF_ERROR;
 1064                                   uint16_T PRINTF_SIZE;
 1065                                   {
 1066                                       uint8_T * simDataMemUnitPtr;
 1067                                       simDataMemUnitPtr = (uint8_T *) &PRINTF_ERROR;
 1068                                       {
 1069                                           size_t num_elements = 1;
 1070                                           size_t bytes_consumed = num_elements * sizeof(uint8_T);
 1071                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1072                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1073                                       }
 1074                                   }
 1075                                   {
 1076                                       uint8_T * simDataMemUnitPtr;
 1077                                       simDataMemUnitPtr = (uint8_T *) &PRINTF_SIZE;
 1078                                       {
 1079                                           size_t num_elements = 1;
 1080                                           size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1081                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1082                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1083                                       }
 1084                                   }
 1085                                   if (PRINTF_ERROR != TARGET_IO_SUCCESS) {
 1086                                       {
 1087                                           mxArray * prhs[ 2 ];
 1088                                           prhs[0] = mxCreateString("PIL:pil:TargetIOError");
 1089                                           prhs[1] = mxCreateDoubleScalar(PRINTF_ERROR);
 1090                                           callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1091                                           return;
 1092                                       }
 1093                                   } else {
 1094                                       uint8_T *pPrintBuff;
 1095                                       pPrintBuff = mxMemUnitPtr;
 1096                                       if (pPrintBuff[PRINTF_SIZE-1] == '\0') {
 1097                                           mexPrintf("%s", pPrintBuff);
 1098                                       } /* if */
 1099                                   } /* if */
 1100                                   mxMemUnitPtr = mxMemUnitPtr + PRINTF_SIZE;
 1101                                   break;
 1102                               }
 1103                               case RESPONSE_FOPEN: {
 1104                                   uint16_T FOPEN_FID;
 1105                                   uint16_T FOPEN_NAME_SIZE;
 1106                                   targetIOFd_T *targetIOFdPtr;
 1107                                   {
 1108                                       uint8_T * simDataMemUnitPtr;
 1109                                       simDataMemUnitPtr = (uint8_T *) &FOPEN_FID;
 1110                                       {
 1111                                           size_t num_elements = 1;
 1112                                           size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1113                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1114                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1115                                       }
 1116                                   }
 1117                                   {
 1118                                       uint8_T * simDataMemUnitPtr;
 1119                                       simDataMemUnitPtr = (uint8_T *) &FOPEN_NAME_SIZE;
 1120                                       {
 1121                                           size_t num_elements = 1;
 1122                                           size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1123                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1124                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1125                                       }
 1126                                   }
 1127                                   targetIOFdPtr = (targetIOFd_T *) ssGetPWorkValue(S, 2);
 1128                                   if (targetIOFdPtr != NULL) {
 1129                                       /* check fid increments by 1 */
 1130                                       if (targetIOFdPtr->fidOffset + 1 == FOPEN_FID) {
 1131                                           targetIOFdPtr->fidOffset = FOPEN_FID;
 1132                                           growTargetIOFd(S, targetIOFdPtr, targetIOFdPtr->fidOffset + 1);
 1133                                           if (targetIOFdPtr->Fd != NULL)  {
 1134                                               uint8_T *pFopenBuff;
 1135                                               targetIOFdPtr->Fd[targetIOFdPtr->fidOffset] = NULL;
 1136                                               pFopenBuff = mxMemUnitPtr;
 1137                                               if (pFopenBuff[FOPEN_NAME_SIZE-1] == '\0') {
 1138                                                   FILE * tmpFd = NULL;
 1139                                                   tmpFd = fopen((char *) pFopenBuff,"w");
 1140                                                   if (tmpFd != NULL) {
 1141                                                       /* save the file descriptor */
 1142                                                       targetIOFdPtr->Fd[targetIOFdPtr->fidOffset] = tmpFd;
 1143                                                   } else {
 1144                                                       {
 1145                                                           mxArray * prhs[ 2 ];
 1146                                                           prhs[0] = mxCreateString("PIL:pil:TargetIOFopenError");
 1147                                                           prhs[1] = mxCreateString((char *) pFopenBuff);
 1148                                                           callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1149                                                           return;
 1150                                                       }
 1151                                                   } /* if */
 1152                                               } /* if */
 1153                                           } /* if */
 1154                                       } else {
 1155                                           {
 1156                                               mxArray * prhs[ 2 ];
 1157                                               prhs[0] = mxCreateString("PIL:pil:TargetIOFopenFidError");
 1158                                               prhs[1] = mxCreateDoubleScalar(FOPEN_FID);
 1159                                               callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1160                                               return;
 1161                                           }
 1162                                       } /* if */
 1163                                   } /* if */
 1164                                   mxMemUnitPtr = mxMemUnitPtr + FOPEN_NAME_SIZE;
 1165                                   break;
 1166                               }
 1167                               case RESPONSE_FPRINTF: {
 1168                                   uint8_T FPRINTF_ERROR;
 1169                                   uint16_T FPRINTF_FID;
 1170                                   uint16_T FPRINTF_SIZE;
 1171                                   {
 1172                                       uint8_T * simDataMemUnitPtr;
 1173                                       simDataMemUnitPtr = (uint8_T *) &FPRINTF_ERROR;
 1174                                       {
 1175                                           size_t num_elements = 1;
 1176                                           size_t bytes_consumed = num_elements * sizeof(uint8_T);
 1177                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1178                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1179                                       }
 1180                                   }
 1181                                   {
 1182                                       uint8_T * simDataMemUnitPtr;
 1183                                       simDataMemUnitPtr = (uint8_T *) &FPRINTF_FID;
 1184                                       {
 1185                                           size_t num_elements = 1;
 1186                                           size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1187                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1188                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1189                                       }
 1190                                   }
 1191                                   {
 1192                                       uint8_T * simDataMemUnitPtr;
 1193                                       simDataMemUnitPtr = (uint8_T *) &FPRINTF_SIZE;
 1194                                       {
 1195                                           size_t num_elements = 1;
 1196                                           size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1197                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1198                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1199                                       }
 1200                                   }
 1201                                   if (FPRINTF_ERROR != TARGET_IO_SUCCESS) {
 1202                                       {
 1203                                           mxArray * prhs[ 2 ];
 1204                                           prhs[0] = mxCreateString("PIL:pil:TargetIOError");
 1205                                           prhs[1] = mxCreateDoubleScalar(FPRINTF_ERROR);
 1206                                           callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1207                                           return;
 1208                                       }
 1209                                   } else {
 1210                                       targetIOFd_T * targetIOFdPtr = (targetIOFd_T *) ssGetPWorkValue(S, 2);
 1211                                       if (targetIOFdPtr != NULL) {
 1212                                           if (targetIOFdPtr->size > FPRINTF_FID) {
 1213                                               if (targetIOFdPtr->Fd[FPRINTF_FID] != NULL) {
 1214                                                   uint8_T *pFprintfBuff;
 1215                                                   pFprintfBuff = mxMemUnitPtr;
 1216                                                   if (pFprintfBuff[FPRINTF_SIZE-1] == '\0') {
 1217                                                       fprintf(targetIOFdPtr->Fd[FPRINTF_FID], "%s", pFprintfBuff);
 1218                                                   } /* if */
 1219                                               } /* if */
 1220                                           } /* if */
 1221                                       } /* if */
 1222                                   } /* if */
 1223                                   mxMemUnitPtr = mxMemUnitPtr + FPRINTF_SIZE;
 1224                                   break;
 1225                               }
 1226                               case RESPONSE_TYPE_SIZE: {
 1227                                   uint8_T typeId = *mxMemUnitPtr;
 1228                                   uint8_T typeBytes;
 1229                                   mxMemUnitPtr++;
 1230                                   typeBytes = *mxMemUnitPtr;
 1231                                   mxMemUnitPtr++;
 1232                                   switch (typeId) {
 1233                                       case SS_SINGLE: {
 1234                                           if (typeBytes != 4) {
 1235                                               {
 1236                                                   mxArray * prhs[ 3 ];
 1237                                                   prhs[0] = mxCreateString("PIL:pilverification:SingleUnsupported");
 1238                                                   prhs[1] = mxCreateDoubleScalar(4);
 1239                                                   prhs[2] = mxCreateDoubleScalar(typeBytes);
 1240                                                   callMATLAB(S,  0, NULL, 3, prhs, "DAStudio.error", 0);
 1241                                                   return;
 1242                                               }
 1243                                           } /* if */
 1244                                           break;
 1245                                       }
 1246                                       case SS_DOUBLE: {
 1247                                           if (typeBytes != 8) {
 1248                                               {
 1249                                                   mxArray * prhs[ 3 ];
 1250                                                   prhs[0] = mxCreateString("PIL:pilverification:DoubleUnsupported");
 1251                                                   prhs[1] = mxCreateDoubleScalar(8);
 1252                                                   prhs[2] = mxCreateDoubleScalar(typeBytes);
 1253                                                   callMATLAB(S,  0, NULL, 3, prhs, "DAStudio.error", 0);
 1254                                                   return;
 1255                                               }
 1256                                           } /* if */
 1257                                           break;
 1258                                       }
 1259                                       default: {
 1260                                           {
 1261                                               mxArray * prhs[ 2 ];
 1262                                               prhs[0] = mxCreateString("PIL:pilverification:UnknownTypeId");
 1263                                               prhs[1] = mxCreateDoubleScalar(typeId);
 1264                                               callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1265                                               return;
 1266                                           }
 1267                                           break;
 1268                                       }
 1269                                   } /* switch */
 1270                                   break;
 1271                               }
 1272                               default: {
 1273                                   {
 1274                                       mxArray * prhs[ 2 ];
 1275                                       prhs[0] = mxCreateString("PIL:pilverification:UnknownResponseId");
 1276                                       prhs[1] = mxCreateDoubleScalar(responseId);
 1277                                       callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1278                                       return;
 1279                                   }
 1280                                   break;
 1281                               }
 1282                           } /* switch */
 1283                       } /* while */
 1284                   } /* if */
 1285               } /* if */
 1286           } /* if */
 1287       }
 1288       /* initialize parameters */
 1289       mdlProcessParameters(S);
 1290       {
 1291           uint8_T * mxMemUnitPtr;
 1292           mwSize dataInSize = 0;
 1293           IOBuffer_T * IOBufferPtr = (IOBuffer_T *) ssGetPWorkValue(S, 1);
 1294           if (IOBufferPtr != NULL) {
 1295               growIOBuffer(S, IOBufferPtr, 5);
 1296               if (IOBufferPtr->data != NULL) {
 1297                   mxMemUnitPtr = (uint8_T *) IOBufferPtr->data;
 1298                   /* write command id */
 1299                   *mxMemUnitPtr = 1;
 1300                   mxMemUnitPtr++;
 1301                   {
 1302                       uint8_T * simDataMemUnitPtr;
 1303                       uint32_T commandDataFcnid = 0;
 1304                       simDataMemUnitPtr = (uint8_T *) &commandDataFcnid;
 1305                       {
 1306                           size_t num_elements = 1;
 1307                           size_t bytes_consumed = num_elements * sizeof(uint32_T);
 1308                           memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 1309                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1310                       }
 1311                   }
 1312                   {
 1313                       int errorOccurred;
 1314                       errorOccurred = commandDispatch(S, IOBufferPtr, 5, &dataInSize, 0);
 1315                       if (errorOccurred) {
 1316                           return;
 1317                       } /* if */
 1318                   }
 1319                   if (dataInSize > 0) {
 1320                       size_t dataInMemUnitSize = dataInSize;
 1321                       uint8_T responseId;
 1322                       uint8_T * mxMemUnitPtrEnd;
 1323                       #define RESPONSE_ERROR 0
 1324                       #define RESPONSE_OUTPUT_DATA 1
 1325                       #define RESPONSE_PRINTF 3
 1326                       #define RESPONSE_FOPEN 4
 1327                       #define RESPONSE_FPRINTF 5
 1328                       #define TARGET_IO_SUCCESS 0
 1329                       mxMemUnitPtr = (uint8_T *) IOBufferPtr->data;
 1330                       mxMemUnitPtrEnd = mxMemUnitPtr + dataInMemUnitSize - 1;
 1331                       while (mxMemUnitPtr <= mxMemUnitPtrEnd) {
 1332                           /* read response id */
 1333                           responseId = *mxMemUnitPtr;
 1334                           mxMemUnitPtr++;
 1335                           switch (responseId) {
 1336                               case RESPONSE_ERROR: {
 1337                                   uint8_T errorId = *mxMemUnitPtr;
 1338                                   mxMemUnitPtr++;
 1339                                   if (errorId) {
 1340                                       {
 1341                                           mxArray * prhs[ 2 ];
 1342                                           prhs[0] = mxCreateString("PIL:pilverification:PILError");
 1343                                           prhs[1] = mxCreateDoubleScalar(errorId);
 1344                                           callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1345                                           return;
 1346                                       }
 1347                                   } /* if */
 1348                                   break;
 1349                               }
 1350                               case RESPONSE_PRINTF: {
 1351                                   uint8_T PRINTF_ERROR;
 1352                                   uint16_T PRINTF_SIZE;
 1353                                   {
 1354                                       uint8_T * simDataMemUnitPtr;
 1355                                       simDataMemUnitPtr = (uint8_T *) &PRINTF_ERROR;
 1356                                       {
 1357                                           size_t num_elements = 1;
 1358                                           size_t bytes_consumed = num_elements * sizeof(uint8_T);
 1359                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1360                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1361                                       }
 1362                                   }
 1363                                   {
 1364                                       uint8_T * simDataMemUnitPtr;
 1365                                       simDataMemUnitPtr = (uint8_T *) &PRINTF_SIZE;
 1366                                       {
 1367                                           size_t num_elements = 1;
 1368                                           size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1369                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1370                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1371                                       }
 1372                                   }
 1373                                   if (PRINTF_ERROR != TARGET_IO_SUCCESS) {
 1374                                       {
 1375                                           mxArray * prhs[ 2 ];
 1376                                           prhs[0] = mxCreateString("PIL:pil:TargetIOError");
 1377                                           prhs[1] = mxCreateDoubleScalar(PRINTF_ERROR);
 1378                                           callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1379                                           return;
 1380                                       }
 1381                                   } else {
 1382                                       uint8_T *pPrintBuff;
 1383                                       pPrintBuff = mxMemUnitPtr;
 1384                                       if (pPrintBuff[PRINTF_SIZE-1] == '\0') {
 1385                                           mexPrintf("%s", pPrintBuff);
 1386                                       } /* if */
 1387                                   } /* if */
 1388                                   mxMemUnitPtr = mxMemUnitPtr + PRINTF_SIZE;
 1389                                   break;
 1390                               }
 1391                               case RESPONSE_FOPEN: {
 1392                                   uint16_T FOPEN_FID;
 1393                                   uint16_T FOPEN_NAME_SIZE;
 1394                                   targetIOFd_T *targetIOFdPtr;
 1395                                   {
 1396                                       uint8_T * simDataMemUnitPtr;
 1397                                       simDataMemUnitPtr = (uint8_T *) &FOPEN_FID;
 1398                                       {
 1399                                           size_t num_elements = 1;
 1400                                           size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1401                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1402                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1403                                       }
 1404                                   }
 1405                                   {
 1406                                       uint8_T * simDataMemUnitPtr;
 1407                                       simDataMemUnitPtr = (uint8_T *) &FOPEN_NAME_SIZE;
 1408                                       {
 1409                                           size_t num_elements = 1;
 1410                                           size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1411                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1412                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1413                                       }
 1414                                   }
 1415                                   targetIOFdPtr = (targetIOFd_T *) ssGetPWorkValue(S, 2);
 1416                                   if (targetIOFdPtr != NULL) {
 1417                                       /* check fid increments by 1 */
 1418                                       if (targetIOFdPtr->fidOffset + 1 == FOPEN_FID) {
 1419                                           targetIOFdPtr->fidOffset = FOPEN_FID;
 1420                                           growTargetIOFd(S, targetIOFdPtr, targetIOFdPtr->fidOffset + 1);
 1421                                           if (targetIOFdPtr->Fd != NULL)  {
 1422                                               uint8_T *pFopenBuff;
 1423                                               targetIOFdPtr->Fd[targetIOFdPtr->fidOffset] = NULL;
 1424                                               pFopenBuff = mxMemUnitPtr;
 1425                                               if (pFopenBuff[FOPEN_NAME_SIZE-1] == '\0') {
 1426                                                   FILE * tmpFd = NULL;
 1427                                                   tmpFd = fopen((char *) pFopenBuff,"w");
 1428                                                   if (tmpFd != NULL) {
 1429                                                       /* save the file descriptor */
 1430                                                       targetIOFdPtr->Fd[targetIOFdPtr->fidOffset] = tmpFd;
 1431                                                   } else {
 1432                                                       {
 1433                                                           mxArray * prhs[ 2 ];
 1434                                                           prhs[0] = mxCreateString("PIL:pil:TargetIOFopenError");
 1435                                                           prhs[1] = mxCreateString((char *) pFopenBuff);
 1436                                                           callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1437                                                           return;
 1438                                                       }
 1439                                                   } /* if */
 1440                                               } /* if */
 1441                                           } /* if */
 1442                                       } else {
 1443                                           {
 1444                                               mxArray * prhs[ 2 ];
 1445                                               prhs[0] = mxCreateString("PIL:pil:TargetIOFopenFidError");
 1446                                               prhs[1] = mxCreateDoubleScalar(FOPEN_FID);
 1447                                               callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1448                                               return;
 1449                                           }
 1450                                       } /* if */
 1451                                   } /* if */
 1452                                   mxMemUnitPtr = mxMemUnitPtr + FOPEN_NAME_SIZE;
 1453                                   break;
 1454                               }
 1455                               case RESPONSE_FPRINTF: {
 1456                                   uint8_T FPRINTF_ERROR;
 1457                                   uint16_T FPRINTF_FID;
 1458                                   uint16_T FPRINTF_SIZE;
 1459                                   {
 1460                                       uint8_T * simDataMemUnitPtr;
 1461                                       simDataMemUnitPtr = (uint8_T *) &FPRINTF_ERROR;
 1462                                       {
 1463                                           size_t num_elements = 1;
 1464                                           size_t bytes_consumed = num_elements * sizeof(uint8_T);
 1465                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1466                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1467                                       }
 1468                                   }
 1469                                   {
 1470                                       uint8_T * simDataMemUnitPtr;
 1471                                       simDataMemUnitPtr = (uint8_T *) &FPRINTF_FID;
 1472                                       {
 1473                                           size_t num_elements = 1;
 1474                                           size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1475                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1476                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1477                                       }
 1478                                   }
 1479                                   {
 1480                                       uint8_T * simDataMemUnitPtr;
 1481                                       simDataMemUnitPtr = (uint8_T *) &FPRINTF_SIZE;
 1482                                       {
 1483                                           size_t num_elements = 1;
 1484                                           size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1485                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1486                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1487                                       }
 1488                                   }
 1489                                   if (FPRINTF_ERROR != TARGET_IO_SUCCESS) {
 1490                                       {
 1491                                           mxArray * prhs[ 2 ];
 1492                                           prhs[0] = mxCreateString("PIL:pil:TargetIOError");
 1493                                           prhs[1] = mxCreateDoubleScalar(FPRINTF_ERROR);
 1494                                           callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1495                                           return;
 1496                                       }
 1497                                   } else {
 1498                                       targetIOFd_T * targetIOFdPtr = (targetIOFd_T *) ssGetPWorkValue(S, 2);
 1499                                       if (targetIOFdPtr != NULL) {
 1500                                           if (targetIOFdPtr->size > FPRINTF_FID) {
 1501                                               if (targetIOFdPtr->Fd[FPRINTF_FID] != NULL) {
 1502                                                   uint8_T *pFprintfBuff;
 1503                                                   pFprintfBuff = mxMemUnitPtr;
 1504                                                   if (pFprintfBuff[FPRINTF_SIZE-1] == '\0') {
 1505                                                       fprintf(targetIOFdPtr->Fd[FPRINTF_FID], "%s", pFprintfBuff);
 1506                                                   } /* if */
 1507                                               } /* if */
 1508                                           } /* if */
 1509                                       } /* if */
 1510                                   } /* if */
 1511                                   mxMemUnitPtr = mxMemUnitPtr + FPRINTF_SIZE;
 1512                                   break;
 1513                               }
 1514                               case RESPONSE_OUTPUT_DATA: {
 1515                                   {
 1516                                       uint8_T * simDataMemUnitPtr;
 1517                                       simDataMemUnitPtr = ( uint8_T *) ssGetOutputPortSignal(S, 0);
 1518                                       {
 1519                                           size_t num_elements = 1;
 1520                                           size_t bytes_consumed = num_elements * sizeof(real64_T);
 1521                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1522                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1523                                       }
 1524                                   }
 1525                                   {
 1526                                       uint8_T * simDataMemUnitPtr;
 1527                                       simDataMemUnitPtr = ( uint8_T *) ssGetOutputPortSignal(S, 1);
 1528                                       {
 1529                                           size_t num_elements = 1;
 1530                                           size_t bytes_consumed = num_elements * sizeof(real64_T);
 1531                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1532                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1533                                       }
 1534                                   }
 1535                                   {
 1536                                       uint8_T * simDataMemUnitPtr;
 1537                                       simDataMemUnitPtr = ( uint8_T *) ssGetOutputPortSignal(S, 2);
 1538                                       {
 1539                                           size_t num_elements = 1;
 1540                                           size_t bytes_consumed = num_elements * sizeof(real64_T);
 1541                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1542                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1543                                       }
 1544                                   }
 1545                                   break;
 1546                               }
 1547                               default: {
 1548                                   {
 1549                                       mxArray * prhs[ 2 ];
 1550                                       prhs[0] = mxCreateString("PIL:pilverification:UnknownResponseId");
 1551                                       prhs[1] = mxCreateDoubleScalar(responseId);
 1552                                       callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1553                                       return;
 1554                                   }
 1555                                   break;
 1556                               }
 1557                           } /* switch */
 1558                       } /* while */
 1559                   } /* if */
 1560               } /* if */
 1561           } /* if */
 1562       }
 1563   }
 1564   #endif /* MDL_START */
 1565   
 1566   #if defined(MDL_PROCESS_PARAMETERS)
 1567   static void mdlProcessParameters(SimStruct *S) {
 1568       int isOkToProcessParams;
 1569       {
 1570           {
 1571               mxArray *rhs[3];
 1572               mxArray *lhs[1];
 1573               char * simulinkBlockPath = getSimulinkBlockPath(S);
 1574               if (simulinkBlockPath == NULL) {
 1575                   ssSetErrorStatus(S, "ModelBlock SIL/PIL unexpected error: getSimulinkBlockPath returned NULL pointer. Check search string was found in ssGetPath.\n");
 1576                   return;
 1577               }
 1578               rhs[ 0 ] = mxCreateString("@coder.connectivity.SimulinkInterface.getSILPILInterface");
 1579               rhs[ 1 ] = mxCreateDoubleScalar( 1 );
 1580               rhs[ 2 ] = mxCreateString(simulinkBlockPath);
 1581               callMATLAB(S,  1, lhs, 3, rhs, "rtw.pil.SILPILInterface.sfunctionIsOkToProcessParamsHook", 0);
 1582               mxFree((void *) simulinkBlockPath);
 1583               isOkToProcessParams = (int) (*mxGetPr(lhs[0]));
 1584               mxDestroyArray(lhs[0]);
 1585           }
 1586       }
 1587       if (isOkToProcessParams) {
 1588           {
 1589               uint8_T * mxMemUnitPtr;
 1590               mwSize dataInSize = 0;
 1591               IOBuffer_T * IOBufferPtr = (IOBuffer_T *) ssGetPWorkValue(S, 1);
 1592               if (IOBufferPtr != NULL) {
 1593                   growIOBuffer(S, IOBufferPtr, 5);
 1594                   if (IOBufferPtr->data != NULL) {
 1595                       mxMemUnitPtr = (uint8_T *) IOBufferPtr->data;
 1596                       /* write command id */
 1597                       *mxMemUnitPtr = 8;
 1598                       mxMemUnitPtr++;
 1599                       {
 1600                           uint8_T * simDataMemUnitPtr;
 1601                           uint32_T commandDataFcnid = 0;
 1602                           simDataMemUnitPtr = (uint8_T *) &commandDataFcnid;
 1603                           {
 1604                               size_t num_elements = 1;
 1605                               size_t bytes_consumed = num_elements * sizeof(uint32_T);
 1606                               memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 1607                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1608                           }
 1609                       }
 1610                       {
 1611                           int errorOccurred;
 1612                           errorOccurred = commandDispatch(S, IOBufferPtr, 5, &dataInSize, 0);
 1613                           if (errorOccurred) {
 1614                               return;
 1615                           } /* if */
 1616                       }
 1617                       if (dataInSize > 0) {
 1618                           size_t dataInMemUnitSize = dataInSize;
 1619                           uint8_T responseId;
 1620                           uint8_T * mxMemUnitPtrEnd;
 1621                           #define RESPONSE_ERROR 0
 1622                           #define RESPONSE_PRINTF 3
 1623                           #define RESPONSE_FOPEN 4
 1624                           #define RESPONSE_FPRINTF 5
 1625                           #define TARGET_IO_SUCCESS 0
 1626                           mxMemUnitPtr = (uint8_T *) IOBufferPtr->data;
 1627                           mxMemUnitPtrEnd = mxMemUnitPtr + dataInMemUnitSize - 1;
 1628                           while (mxMemUnitPtr <= mxMemUnitPtrEnd) {
 1629                               /* read response id */
 1630                               responseId = *mxMemUnitPtr;
 1631                               mxMemUnitPtr++;
 1632                               switch (responseId) {
 1633                                   case RESPONSE_ERROR: {
 1634                                       uint8_T errorId = *mxMemUnitPtr;
 1635                                       mxMemUnitPtr++;
 1636                                       if (errorId) {
 1637                                           {
 1638                                               mxArray * prhs[ 2 ];
 1639                                               prhs[0] = mxCreateString("PIL:pilverification:PILError");
 1640                                               prhs[1] = mxCreateDoubleScalar(errorId);
 1641                                               callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1642                                               return;
 1643                                           }
 1644                                       } /* if */
 1645                                       break;
 1646                                   }
 1647                                   case RESPONSE_PRINTF: {
 1648                                       uint8_T PRINTF_ERROR;
 1649                                       uint16_T PRINTF_SIZE;
 1650                                       {
 1651                                           uint8_T * simDataMemUnitPtr;
 1652                                           simDataMemUnitPtr = (uint8_T *) &PRINTF_ERROR;
 1653                                           {
 1654                                               size_t num_elements = 1;
 1655                                               size_t bytes_consumed = num_elements * sizeof(uint8_T);
 1656                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1657                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1658                                           }
 1659                                       }
 1660                                       {
 1661                                           uint8_T * simDataMemUnitPtr;
 1662                                           simDataMemUnitPtr = (uint8_T *) &PRINTF_SIZE;
 1663                                           {
 1664                                               size_t num_elements = 1;
 1665                                               size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1666                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1667                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1668                                           }
 1669                                       }
 1670                                       if (PRINTF_ERROR != TARGET_IO_SUCCESS) {
 1671                                           {
 1672                                               mxArray * prhs[ 2 ];
 1673                                               prhs[0] = mxCreateString("PIL:pil:TargetIOError");
 1674                                               prhs[1] = mxCreateDoubleScalar(PRINTF_ERROR);
 1675                                               callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1676                                               return;
 1677                                           }
 1678                                       } else {
 1679                                           uint8_T *pPrintBuff;
 1680                                           pPrintBuff = mxMemUnitPtr;
 1681                                           if (pPrintBuff[PRINTF_SIZE-1] == '\0') {
 1682                                               mexPrintf("%s", pPrintBuff);
 1683                                           } /* if */
 1684                                       } /* if */
 1685                                       mxMemUnitPtr = mxMemUnitPtr + PRINTF_SIZE;
 1686                                       break;
 1687                                   }
 1688                                   case RESPONSE_FOPEN: {
 1689                                       uint16_T FOPEN_FID;
 1690                                       uint16_T FOPEN_NAME_SIZE;
 1691                                       targetIOFd_T *targetIOFdPtr;
 1692                                       {
 1693                                           uint8_T * simDataMemUnitPtr;
 1694                                           simDataMemUnitPtr = (uint8_T *) &FOPEN_FID;
 1695                                           {
 1696                                               size_t num_elements = 1;
 1697                                               size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1698                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1699                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1700                                           }
 1701                                       }
 1702                                       {
 1703                                           uint8_T * simDataMemUnitPtr;
 1704                                           simDataMemUnitPtr = (uint8_T *) &FOPEN_NAME_SIZE;
 1705                                           {
 1706                                               size_t num_elements = 1;
 1707                                               size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1708                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1709                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1710                                           }
 1711                                       }
 1712                                       targetIOFdPtr = (targetIOFd_T *) ssGetPWorkValue(S, 2);
 1713                                       if (targetIOFdPtr != NULL) {
 1714                                           /* check fid increments by 1 */
 1715                                           if (targetIOFdPtr->fidOffset + 1 == FOPEN_FID) {
 1716                                               targetIOFdPtr->fidOffset = FOPEN_FID;
 1717                                               growTargetIOFd(S, targetIOFdPtr, targetIOFdPtr->fidOffset + 1);
 1718                                               if (targetIOFdPtr->Fd != NULL)  {
 1719                                                   uint8_T *pFopenBuff;
 1720                                                   targetIOFdPtr->Fd[targetIOFdPtr->fidOffset] = NULL;
 1721                                                   pFopenBuff = mxMemUnitPtr;
 1722                                                   if (pFopenBuff[FOPEN_NAME_SIZE-1] == '\0') {
 1723                                                       FILE * tmpFd = NULL;
 1724                                                       tmpFd = fopen((char *) pFopenBuff,"w");
 1725                                                       if (tmpFd != NULL) {
 1726                                                           /* save the file descriptor */
 1727                                                           targetIOFdPtr->Fd[targetIOFdPtr->fidOffset] = tmpFd;
 1728                                                       } else {
 1729                                                           {
 1730                                                               mxArray * prhs[ 2 ];
 1731                                                               prhs[0] = mxCreateString("PIL:pil:TargetIOFopenError");
 1732                                                               prhs[1] = mxCreateString((char *) pFopenBuff);
 1733                                                               callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1734                                                               return;
 1735                                                           }
 1736                                                       } /* if */
 1737                                                   } /* if */
 1738                                               } /* if */
 1739                                           } else {
 1740                                               {
 1741                                                   mxArray * prhs[ 2 ];
 1742                                                   prhs[0] = mxCreateString("PIL:pil:TargetIOFopenFidError");
 1743                                                   prhs[1] = mxCreateDoubleScalar(FOPEN_FID);
 1744                                                   callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1745                                                   return;
 1746                                               }
 1747                                           } /* if */
 1748                                       } /* if */
 1749                                       mxMemUnitPtr = mxMemUnitPtr + FOPEN_NAME_SIZE;
 1750                                       break;
 1751                                   }
 1752                                   case RESPONSE_FPRINTF: {
 1753                                       uint8_T FPRINTF_ERROR;
 1754                                       uint16_T FPRINTF_FID;
 1755                                       uint16_T FPRINTF_SIZE;
 1756                                       {
 1757                                           uint8_T * simDataMemUnitPtr;
 1758                                           simDataMemUnitPtr = (uint8_T *) &FPRINTF_ERROR;
 1759                                           {
 1760                                               size_t num_elements = 1;
 1761                                               size_t bytes_consumed = num_elements * sizeof(uint8_T);
 1762                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1763                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1764                                           }
 1765                                       }
 1766                                       {
 1767                                           uint8_T * simDataMemUnitPtr;
 1768                                           simDataMemUnitPtr = (uint8_T *) &FPRINTF_FID;
 1769                                           {
 1770                                               size_t num_elements = 1;
 1771                                               size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1772                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1773                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1774                                           }
 1775                                       }
 1776                                       {
 1777                                           uint8_T * simDataMemUnitPtr;
 1778                                           simDataMemUnitPtr = (uint8_T *) &FPRINTF_SIZE;
 1779                                           {
 1780                                               size_t num_elements = 1;
 1781                                               size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1782                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1783                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1784                                           }
 1785                                       }
 1786                                       if (FPRINTF_ERROR != TARGET_IO_SUCCESS) {
 1787                                           {
 1788                                               mxArray * prhs[ 2 ];
 1789                                               prhs[0] = mxCreateString("PIL:pil:TargetIOError");
 1790                                               prhs[1] = mxCreateDoubleScalar(FPRINTF_ERROR);
 1791                                               callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1792                                               return;
 1793                                           }
 1794                                       } else {
 1795                                           targetIOFd_T * targetIOFdPtr = (targetIOFd_T *) ssGetPWorkValue(S, 2);
 1796                                           if (targetIOFdPtr != NULL) {
 1797                                               if (targetIOFdPtr->size > FPRINTF_FID) {
 1798                                                   if (targetIOFdPtr->Fd[FPRINTF_FID] != NULL) {
 1799                                                       uint8_T *pFprintfBuff;
 1800                                                       pFprintfBuff = mxMemUnitPtr;
 1801                                                       if (pFprintfBuff[FPRINTF_SIZE-1] == '\0') {
 1802                                                           fprintf(targetIOFdPtr->Fd[FPRINTF_FID], "%s", pFprintfBuff);
 1803                                                       } /* if */
 1804                                                   } /* if */
 1805                                               } /* if */
 1806                                           } /* if */
 1807                                       } /* if */
 1808                                       mxMemUnitPtr = mxMemUnitPtr + FPRINTF_SIZE;
 1809                                       break;
 1810                                   }
 1811                                   default: {
 1812                                       {
 1813                                           mxArray * prhs[ 2 ];
 1814                                           prhs[0] = mxCreateString("PIL:pilverification:UnknownResponseId");
 1815                                           prhs[1] = mxCreateDoubleScalar(responseId);
 1816                                           callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1817                                           return;
 1818                                       }
 1819                                       break;
 1820                                   }
 1821                               } /* switch */
 1822                           } /* while */
 1823                       } /* if */
 1824                   } /* if */
 1825               } /* if */
 1826           }
 1827       } /* if */
 1828   }
 1829   #endif /* MDL_PROCESS_PARAMETERS */
 1830   
 1831   #define MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
 1832   #if defined(MDL_INITIALIZE_CONDITIONS)
 1833   static void mdlInitializeConditions(SimStruct *S) {
 1834       if (!ssIsFirstInitCond(S)) {
 1835          ssSetErrorStatus(S, "This In-the-Loop block cannot be called from within a subsystem that can reset its states because the target code has been optimized to remove zero initialization of states. Turn off optimization setting 'Optimize initialization code for model reference' on the 'Optimization' page of the Configuration Parameters dialog of the referenced model to use this block in a subsystem that can reset its states.");
 1836          return;
 1837       }
 1838       {
 1839           uint8_T * mxMemUnitPtr;
 1840           mwSize dataInSize = 0;
 1841           IOBuffer_T * IOBufferPtr = (IOBuffer_T *) ssGetPWorkValue(S, 1);
 1842           if (IOBufferPtr != NULL) {
 1843               growIOBuffer(S, IOBufferPtr, 5);
 1844               if (IOBufferPtr->data != NULL) {
 1845                   mxMemUnitPtr = (uint8_T *) IOBufferPtr->data;
 1846                   /* write command id */
 1847                   *mxMemUnitPtr = 2;
 1848                   mxMemUnitPtr++;
 1849                   {
 1850                       uint8_T * simDataMemUnitPtr;
 1851                       uint32_T commandDataFcnid = 0;
 1852                       simDataMemUnitPtr = (uint8_T *) &commandDataFcnid;
 1853                       {
 1854                           size_t num_elements = 1;
 1855                           size_t bytes_consumed = num_elements * sizeof(uint32_T);
 1856                           memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 1857                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1858                       }
 1859                   }
 1860                   {
 1861                       int errorOccurred;
 1862                       errorOccurred = commandDispatch(S, IOBufferPtr, 5, &dataInSize, 0);
 1863                       if (errorOccurred) {
 1864                           return;
 1865                       } /* if */
 1866                   }
 1867                   if (dataInSize > 0) {
 1868                       size_t dataInMemUnitSize = dataInSize;
 1869                       uint8_T responseId;
 1870                       uint8_T * mxMemUnitPtrEnd;
 1871                       #define RESPONSE_ERROR 0
 1872                       #define RESPONSE_PRINTF 3
 1873                       #define RESPONSE_FOPEN 4
 1874                       #define RESPONSE_FPRINTF 5
 1875                       #define TARGET_IO_SUCCESS 0
 1876                       mxMemUnitPtr = (uint8_T *) IOBufferPtr->data;
 1877                       mxMemUnitPtrEnd = mxMemUnitPtr + dataInMemUnitSize - 1;
 1878                       while (mxMemUnitPtr <= mxMemUnitPtrEnd) {
 1879                           /* read response id */
 1880                           responseId = *mxMemUnitPtr;
 1881                           mxMemUnitPtr++;
 1882                           switch (responseId) {
 1883                               case RESPONSE_ERROR: {
 1884                                   uint8_T errorId = *mxMemUnitPtr;
 1885                                   mxMemUnitPtr++;
 1886                                   if (errorId) {
 1887                                       {
 1888                                           mxArray * prhs[ 2 ];
 1889                                           prhs[0] = mxCreateString("PIL:pilverification:PILError");
 1890                                           prhs[1] = mxCreateDoubleScalar(errorId);
 1891                                           callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1892                                           return;
 1893                                       }
 1894                                   } /* if */
 1895                                   break;
 1896                               }
 1897                               case RESPONSE_PRINTF: {
 1898                                   uint8_T PRINTF_ERROR;
 1899                                   uint16_T PRINTF_SIZE;
 1900                                   {
 1901                                       uint8_T * simDataMemUnitPtr;
 1902                                       simDataMemUnitPtr = (uint8_T *) &PRINTF_ERROR;
 1903                                       {
 1904                                           size_t num_elements = 1;
 1905                                           size_t bytes_consumed = num_elements * sizeof(uint8_T);
 1906                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1907                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1908                                       }
 1909                                   }
 1910                                   {
 1911                                       uint8_T * simDataMemUnitPtr;
 1912                                       simDataMemUnitPtr = (uint8_T *) &PRINTF_SIZE;
 1913                                       {
 1914                                           size_t num_elements = 1;
 1915                                           size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1916                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1917                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1918                                       }
 1919                                   }
 1920                                   if (PRINTF_ERROR != TARGET_IO_SUCCESS) {
 1921                                       {
 1922                                           mxArray * prhs[ 2 ];
 1923                                           prhs[0] = mxCreateString("PIL:pil:TargetIOError");
 1924                                           prhs[1] = mxCreateDoubleScalar(PRINTF_ERROR);
 1925                                           callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1926                                           return;
 1927                                       }
 1928                                   } else {
 1929                                       uint8_T *pPrintBuff;
 1930                                       pPrintBuff = mxMemUnitPtr;
 1931                                       if (pPrintBuff[PRINTF_SIZE-1] == '\0') {
 1932                                           mexPrintf("%s", pPrintBuff);
 1933                                       } /* if */
 1934                                   } /* if */
 1935                                   mxMemUnitPtr = mxMemUnitPtr + PRINTF_SIZE;
 1936                                   break;
 1937                               }
 1938                               case RESPONSE_FOPEN: {
 1939                                   uint16_T FOPEN_FID;
 1940                                   uint16_T FOPEN_NAME_SIZE;
 1941                                   targetIOFd_T *targetIOFdPtr;
 1942                                   {
 1943                                       uint8_T * simDataMemUnitPtr;
 1944                                       simDataMemUnitPtr = (uint8_T *) &FOPEN_FID;
 1945                                       {
 1946                                           size_t num_elements = 1;
 1947                                           size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1948                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1949                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1950                                       }
 1951                                   }
 1952                                   {
 1953                                       uint8_T * simDataMemUnitPtr;
 1954                                       simDataMemUnitPtr = (uint8_T *) &FOPEN_NAME_SIZE;
 1955                                       {
 1956                                           size_t num_elements = 1;
 1957                                           size_t bytes_consumed = num_elements * sizeof(uint16_T);
 1958                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 1959                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 1960                                       }
 1961                                   }
 1962                                   targetIOFdPtr = (targetIOFd_T *) ssGetPWorkValue(S, 2);
 1963                                   if (targetIOFdPtr != NULL) {
 1964                                       /* check fid increments by 1 */
 1965                                       if (targetIOFdPtr->fidOffset + 1 == FOPEN_FID) {
 1966                                           targetIOFdPtr->fidOffset = FOPEN_FID;
 1967                                           growTargetIOFd(S, targetIOFdPtr, targetIOFdPtr->fidOffset + 1);
 1968                                           if (targetIOFdPtr->Fd != NULL)  {
 1969                                               uint8_T *pFopenBuff;
 1970                                               targetIOFdPtr->Fd[targetIOFdPtr->fidOffset] = NULL;
 1971                                               pFopenBuff = mxMemUnitPtr;
 1972                                               if (pFopenBuff[FOPEN_NAME_SIZE-1] == '\0') {
 1973                                                   FILE * tmpFd = NULL;
 1974                                                   tmpFd = fopen((char *) pFopenBuff,"w");
 1975                                                   if (tmpFd != NULL) {
 1976                                                       /* save the file descriptor */
 1977                                                       targetIOFdPtr->Fd[targetIOFdPtr->fidOffset] = tmpFd;
 1978                                                   } else {
 1979                                                       {
 1980                                                           mxArray * prhs[ 2 ];
 1981                                                           prhs[0] = mxCreateString("PIL:pil:TargetIOFopenError");
 1982                                                           prhs[1] = mxCreateString((char *) pFopenBuff);
 1983                                                           callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1984                                                           return;
 1985                                                       }
 1986                                                   } /* if */
 1987                                               } /* if */
 1988                                           } /* if */
 1989                                       } else {
 1990                                           {
 1991                                               mxArray * prhs[ 2 ];
 1992                                               prhs[0] = mxCreateString("PIL:pil:TargetIOFopenFidError");
 1993                                               prhs[1] = mxCreateDoubleScalar(FOPEN_FID);
 1994                                               callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 1995                                               return;
 1996                                           }
 1997                                       } /* if */
 1998                                   } /* if */
 1999                                   mxMemUnitPtr = mxMemUnitPtr + FOPEN_NAME_SIZE;
 2000                                   break;
 2001                               }
 2002                               case RESPONSE_FPRINTF: {
 2003                                   uint8_T FPRINTF_ERROR;
 2004                                   uint16_T FPRINTF_FID;
 2005                                   uint16_T FPRINTF_SIZE;
 2006                                   {
 2007                                       uint8_T * simDataMemUnitPtr;
 2008                                       simDataMemUnitPtr = (uint8_T *) &FPRINTF_ERROR;
 2009                                       {
 2010                                           size_t num_elements = 1;
 2011                                           size_t bytes_consumed = num_elements * sizeof(uint8_T);
 2012                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2013                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2014                                       }
 2015                                   }
 2016                                   {
 2017                                       uint8_T * simDataMemUnitPtr;
 2018                                       simDataMemUnitPtr = (uint8_T *) &FPRINTF_FID;
 2019                                       {
 2020                                           size_t num_elements = 1;
 2021                                           size_t bytes_consumed = num_elements * sizeof(uint16_T);
 2022                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2023                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2024                                       }
 2025                                   }
 2026                                   {
 2027                                       uint8_T * simDataMemUnitPtr;
 2028                                       simDataMemUnitPtr = (uint8_T *) &FPRINTF_SIZE;
 2029                                       {
 2030                                           size_t num_elements = 1;
 2031                                           size_t bytes_consumed = num_elements * sizeof(uint16_T);
 2032                                           memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2033                                           mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2034                                       }
 2035                                   }
 2036                                   if (FPRINTF_ERROR != TARGET_IO_SUCCESS) {
 2037                                       {
 2038                                           mxArray * prhs[ 2 ];
 2039                                           prhs[0] = mxCreateString("PIL:pil:TargetIOError");
 2040                                           prhs[1] = mxCreateDoubleScalar(FPRINTF_ERROR);
 2041                                           callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 2042                                           return;
 2043                                       }
 2044                                   } else {
 2045                                       targetIOFd_T * targetIOFdPtr = (targetIOFd_T *) ssGetPWorkValue(S, 2);
 2046                                       if (targetIOFdPtr != NULL) {
 2047                                           if (targetIOFdPtr->size > FPRINTF_FID) {
 2048                                               if (targetIOFdPtr->Fd[FPRINTF_FID] != NULL) {
 2049                                                   uint8_T *pFprintfBuff;
 2050                                                   pFprintfBuff = mxMemUnitPtr;
 2051                                                   if (pFprintfBuff[FPRINTF_SIZE-1] == '\0') {
 2052                                                       fprintf(targetIOFdPtr->Fd[FPRINTF_FID], "%s", pFprintfBuff);
 2053                                                   } /* if */
 2054                                               } /* if */
 2055                                           } /* if */
 2056                                       } /* if */
 2057                                   } /* if */
 2058                                   mxMemUnitPtr = mxMemUnitPtr + FPRINTF_SIZE;
 2059                                   break;
 2060                               }
 2061                               default: {
 2062                                   {
 2063                                       mxArray * prhs[ 2 ];
 2064                                       prhs[0] = mxCreateString("PIL:pilverification:UnknownResponseId");
 2065                                       prhs[1] = mxCreateDoubleScalar(responseId);
 2066                                       callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 2067                                       return;
 2068                                   }
 2069                                   break;
 2070                               }
 2071                           } /* switch */
 2072                       } /* while */
 2073                   } /* if */
 2074               } /* if */
 2075           } /* if */
 2076       }
 2077   }
 2078   #endif /* MDL_INITIALIZE_CONDITIONS */
 2079   
 2080   #define MDL_SET_WORK_WIDTHS   /* Change to #undef to remove function */
 2081   #if defined(MDL_SET_WORK_WIDTHS)
 2082   static void mdlSetWorkWidths(SimStruct *S) {
 2083   }
 2084   #endif /* MDL_SET_WORK_WIDTHS */
 2085   
 2086   static void mdlOutputs(SimStruct *S, int_T tid) {
 2087       /* Singlerate scheduling */
 2088       /* check for sample time hit associated with task 1 */
 2089       if (ssIsSampleHit(S, ssGetInputPortSampleTimeIndex(S, 0), tid)) {
 2090           {
 2091               uint8_T * mxMemUnitPtr;
 2092               mwSize dataInSize = 0;
 2093               IOBuffer_T * IOBufferPtr = (IOBuffer_T *) ssGetPWorkValue(S, 1);
 2094               if (IOBufferPtr != NULL) {
 2095                   growIOBuffer(S, IOBufferPtr, 132);
 2096                   if (IOBufferPtr->data != NULL) {
 2097                       mxMemUnitPtr = (uint8_T *) IOBufferPtr->data;
 2098                       /* write command id */
 2099                       *mxMemUnitPtr = 3;
 2100                       mxMemUnitPtr++;
 2101                       {
 2102                           uint8_T * simDataMemUnitPtr;
 2103                           uint32_T commandDataFcnidTID[2] = {0, 1};
 2104                           simDataMemUnitPtr = (uint8_T *) &commandDataFcnidTID[0];
 2105                           {
 2106                               size_t num_elements = 2;
 2107                               size_t bytes_consumed = num_elements * sizeof(uint32_T);
 2108                               memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2109                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2110                           }
 2111                       }
 2112                       {
 2113                           uint8_T * simDataMemUnitPtr;
 2114                           slDataTypeAccess * dta = ssGetDataTypeAccess(S);
 2115                           const char * bpath = ssGetPath(S);
 2116                           {
 2117                               simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 0);
 2118                               simDataMemUnitPtr = (uint8_T*) (((char *) simDataMemUnitPtr) +
 2119      dtaGetDataTypeElementOffset(dta, bpath, ssGetDataTypeId(S, "slBus1"), 0));
 2120                               {
 2121                                   size_t num_elements = 1;
 2122                                   size_t bytes_consumed = num_elements * sizeof(real64_T);
 2123                                   memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2124                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2125                               }
 2126                           }
 2127                       }
 2128                       {
 2129                           uint8_T * simDataMemUnitPtr;
 2130                           slDataTypeAccess * dta = ssGetDataTypeAccess(S);
 2131                           const char * bpath = ssGetPath(S);
 2132                           {
 2133                               simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 0);
 2134                               simDataMemUnitPtr = (uint8_T*) (((char *) simDataMemUnitPtr) +
 2135      dtaGetDataTypeElementOffset(dta, bpath, ssGetDataTypeId(S, "slBus1"), 1));
 2136                               {
 2137                                   size_t num_elements = 1;
 2138                                   size_t bytes_consumed = num_elements * sizeof(real64_T);
 2139                                   memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2140                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2141                               }
 2142                           }
 2143                       }
 2144                       {
 2145                           uint8_T * simDataMemUnitPtr;
 2146                           slDataTypeAccess * dta = ssGetDataTypeAccess(S);
 2147                           const char * bpath = ssGetPath(S);
 2148                           {
 2149                               simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 0);
 2150                               simDataMemUnitPtr = (uint8_T*) (((char *) simDataMemUnitPtr) +
 2151      dtaGetDataTypeElementOffset(dta, bpath, ssGetDataTypeId(S, "slBus1"), 2));
 2152                               {
 2153                                   size_t num_elements = 1;
 2154                                   size_t bytes_consumed = num_elements * sizeof(real64_T);
 2155                                   memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2156                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2157                               }
 2158                           }
 2159                       }
 2160                       {
 2161                           uint8_T * simDataMemUnitPtr;
 2162                           slDataTypeAccess * dta = ssGetDataTypeAccess(S);
 2163                           const char * bpath = ssGetPath(S);
 2164                           {
 2165                               simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 0);
 2166                               simDataMemUnitPtr = (uint8_T*) (((char *) simDataMemUnitPtr) +
 2167      dtaGetDataTypeElementOffset(dta, bpath, ssGetDataTypeId(S, "slBus1"), 3));
 2168                               {
 2169                                   size_t num_elements = 1;
 2170                                   size_t bytes_consumed = num_elements * sizeof(real64_T);
 2171                                   memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2172                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2173                               }
 2174                           }
 2175                       }
 2176                       {
 2177                           uint8_T * simDataMemUnitPtr;
 2178                           slDataTypeAccess * dta = ssGetDataTypeAccess(S);
 2179                           const char * bpath = ssGetPath(S);
 2180                           {
 2181                               simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 0);
 2182                               simDataMemUnitPtr = (uint8_T*) (((char *) simDataMemUnitPtr) +
 2183      dtaGetDataTypeElementOffset(dta, bpath, ssGetDataTypeId(S, "slBus1"), 4));
 2184                               {
 2185                                   size_t num_elements = 1;
 2186                                   size_t bytes_consumed = num_elements * sizeof(real64_T);
 2187                                   memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2188                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2189                               }
 2190                           }
 2191                       }
 2192                       {
 2193                           uint8_T * simDataMemUnitPtr;
 2194                           slDataTypeAccess * dta = ssGetDataTypeAccess(S);
 2195                           const char * bpath = ssGetPath(S);
 2196                           {
 2197                               simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 1);
 2198                               simDataMemUnitPtr = (uint8_T*) (((char *) simDataMemUnitPtr) +
 2199      dtaGetDataTypeElementOffset(dta, bpath, ssGetDataTypeId(S, "slBus2"), 0));
 2200                               {
 2201                                   size_t num_elements = 1;
 2202                                   size_t bytes_consumed = num_elements * sizeof(real64_T);
 2203                                   memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2204                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2205                               }
 2206                           }
 2207                       }
 2208                       {
 2209                           uint8_T * simDataMemUnitPtr;
 2210                           slDataTypeAccess * dta = ssGetDataTypeAccess(S);
 2211                           const char * bpath = ssGetPath(S);
 2212                           {
 2213                               simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 1);
 2214                               simDataMemUnitPtr = (uint8_T*) (((char *) simDataMemUnitPtr) +
 2215      dtaGetDataTypeElementOffset(dta, bpath, ssGetDataTypeId(S, "slBus2"), 1));
 2216                               {
 2217                                   size_t num_elements = 1;
 2218                                   size_t bytes_consumed = num_elements * sizeof(real64_T);
 2219                                   memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2220                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2221                               }
 2222                           }
 2223                       }
 2224                       {
 2225                           uint8_T * simDataMemUnitPtr;
 2226                           slDataTypeAccess * dta = ssGetDataTypeAccess(S);
 2227                           const char * bpath = ssGetPath(S);
 2228                           {
 2229                               simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 1);
 2230                               simDataMemUnitPtr = (uint8_T*) (((char *) simDataMemUnitPtr) +
 2231      dtaGetDataTypeElementOffset(dta, bpath, ssGetDataTypeId(S, "slBus2"), 2));
 2232                               {
 2233                                   size_t num_elements = 1;
 2234                                   size_t bytes_consumed = num_elements * sizeof(real64_T);
 2235                                   memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2236                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2237                               }
 2238                           }
 2239                       }
 2240                       {
 2241                           uint8_T * simDataMemUnitPtr;
 2242                           slDataTypeAccess * dta = ssGetDataTypeAccess(S);
 2243                           const char * bpath = ssGetPath(S);
 2244                           {
 2245                               simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 1);
 2246                               simDataMemUnitPtr = (uint8_T*) (((char *) simDataMemUnitPtr) +
 2247      dtaGetDataTypeElementOffset(dta, bpath, ssGetDataTypeId(S, "slBus2"), 3));
 2248                               {
 2249                                   size_t num_elements = 1;
 2250                                   size_t bytes_consumed = num_elements * sizeof(real64_T);
 2251                                   memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2252                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2253                               }
 2254                           }
 2255                       }
 2256                       {
 2257                           uint8_T * simDataMemUnitPtr;
 2258                           slDataTypeAccess * dta = ssGetDataTypeAccess(S);
 2259                           const char * bpath = ssGetPath(S);
 2260                           {
 2261                               simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 1);
 2262                               simDataMemUnitPtr = (uint8_T*) (((char *) simDataMemUnitPtr) +
 2263      dtaGetDataTypeElementOffset(dta, bpath, ssGetDataTypeId(S, "slBus2"), 4));
 2264                               {
 2265                                   size_t num_elements = 1;
 2266                                   size_t bytes_consumed = num_elements * sizeof(real64_T);
 2267                                   memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2268                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2269                               }
 2270                           }
 2271                       }
 2272                       {
 2273                           uint8_T * simDataMemUnitPtr;
 2274                           slDataTypeAccess * dta = ssGetDataTypeAccess(S);
 2275                           const char * bpath = ssGetPath(S);
 2276                           {
 2277                               simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 1);
 2278                               simDataMemUnitPtr = (uint8_T*) (((char *) simDataMemUnitPtr) +
 2279      dtaGetDataTypeElementOffset(dta, bpath, ssGetDataTypeId(S, "slBus2"), 5));
 2280                               {
 2281                                   size_t num_elements = 1;
 2282                                   size_t bytes_consumed = num_elements * sizeof(real64_T);
 2283                                   memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2284                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2285                               }
 2286                           }
 2287                       }
 2288                       {
 2289                           uint8_T * simDataMemUnitPtr;
 2290                           simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 2);
 2291                           {
 2292                               size_t num_elements = 1;
 2293                               size_t bytes_consumed = num_elements * sizeof(uint8_T);
 2294                               memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2295                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2296                           }
 2297                       }
 2298                       {
 2299                           uint8_T * simDataMemUnitPtr;
 2300                           simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 3);
 2301                           {
 2302                               size_t num_elements = 1;
 2303                               size_t bytes_consumed = num_elements * sizeof(uint8_T);
 2304                               memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2305                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2306                           }
 2307                       }
 2308                       {
 2309                           uint8_T * simDataMemUnitPtr;
 2310                           simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 4);
 2311                           {
 2312                               size_t num_elements = 1;
 2313                               size_t bytes_consumed = num_elements * sizeof(uint8_T);
 2314                               memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2315                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2316                           }
 2317                       }
 2318                       {
 2319                           uint8_T * simDataMemUnitPtr;
 2320                           simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 5);
 2321                           {
 2322                               size_t num_elements = 1;
 2323                               size_t bytes_consumed = num_elements * sizeof(real64_T);
 2324                               memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2325                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2326                           }
 2327                       }
 2328                       {
 2329                           uint8_T * simDataMemUnitPtr;
 2330                           simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 6);
 2331                           {
 2332                               size_t num_elements = 1;
 2333                               size_t bytes_consumed = num_elements * sizeof(real64_T);
 2334                               memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2335                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2336                           }
 2337                       }
 2338                       {
 2339                           uint8_T * simDataMemUnitPtr;
 2340                           simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 7);
 2341                           {
 2342                               size_t num_elements = 1;
 2343                               size_t bytes_consumed = num_elements * sizeof(real64_T);
 2344                               memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2345                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2346                           }
 2347                       }
 2348                       {
 2349                           uint8_T * simDataMemUnitPtr;
 2350                           simDataMemUnitPtr = ( uint8_T *) ssGetInputPortSignal(S, 8);
 2351                           {
 2352                               size_t num_elements = 1;
 2353                               size_t bytes_consumed = num_elements * sizeof(real64_T);
 2354                               memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2355                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2356                           }
 2357                       }
 2358                       {
 2359                           int errorOccurred;
 2360                           errorOccurred = commandDispatch(S, IOBufferPtr, 132, &dataInSize, 0);
 2361                           if (errorOccurred) {
 2362                               return;
 2363                           } /* if */
 2364                       }
 2365                       if (dataInSize > 0) {
 2366                           size_t dataInMemUnitSize = dataInSize;
 2367                           uint8_T responseId;
 2368                           uint8_T * mxMemUnitPtrEnd;
 2369                           #define RESPONSE_ERROR 0
 2370                           #define RESPONSE_OUTPUT_DATA 1
 2371                           #define RESPONSE_PRINTF 3
 2372                           #define RESPONSE_FOPEN 4
 2373                           #define RESPONSE_FPRINTF 5
 2374                           #define TARGET_IO_SUCCESS 0
 2375                           mxMemUnitPtr = (uint8_T *) IOBufferPtr->data;
 2376                           mxMemUnitPtrEnd = mxMemUnitPtr + dataInMemUnitSize - 1;
 2377                           while (mxMemUnitPtr <= mxMemUnitPtrEnd) {
 2378                               /* read response id */
 2379                               responseId = *mxMemUnitPtr;
 2380                               mxMemUnitPtr++;
 2381                               switch (responseId) {
 2382                                   case RESPONSE_ERROR: {
 2383                                       uint8_T errorId = *mxMemUnitPtr;
 2384                                       mxMemUnitPtr++;
 2385                                       if (errorId) {
 2386                                           {
 2387                                               mxArray * prhs[ 2 ];
 2388                                               prhs[0] = mxCreateString("PIL:pilverification:PILError");
 2389                                               prhs[1] = mxCreateDoubleScalar(errorId);
 2390                                               callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 2391                                               return;
 2392                                           }
 2393                                       } /* if */
 2394                                       break;
 2395                                   }
 2396                                   case RESPONSE_PRINTF: {
 2397                                       uint8_T PRINTF_ERROR;
 2398                                       uint16_T PRINTF_SIZE;
 2399                                       {
 2400                                           uint8_T * simDataMemUnitPtr;
 2401                                           simDataMemUnitPtr = (uint8_T *) &PRINTF_ERROR;
 2402                                           {
 2403                                               size_t num_elements = 1;
 2404                                               size_t bytes_consumed = num_elements * sizeof(uint8_T);
 2405                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2406                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2407                                           }
 2408                                       }
 2409                                       {
 2410                                           uint8_T * simDataMemUnitPtr;
 2411                                           simDataMemUnitPtr = (uint8_T *) &PRINTF_SIZE;
 2412                                           {
 2413                                               size_t num_elements = 1;
 2414                                               size_t bytes_consumed = num_elements * sizeof(uint16_T);
 2415                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2416                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2417                                           }
 2418                                       }
 2419                                       if (PRINTF_ERROR != TARGET_IO_SUCCESS) {
 2420                                           {
 2421                                               mxArray * prhs[ 2 ];
 2422                                               prhs[0] = mxCreateString("PIL:pil:TargetIOError");
 2423                                               prhs[1] = mxCreateDoubleScalar(PRINTF_ERROR);
 2424                                               callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 2425                                               return;
 2426                                           }
 2427                                       } else {
 2428                                           uint8_T *pPrintBuff;
 2429                                           pPrintBuff = mxMemUnitPtr;
 2430                                           if (pPrintBuff[PRINTF_SIZE-1] == '\0') {
 2431                                               mexPrintf("%s", pPrintBuff);
 2432                                           } /* if */
 2433                                       } /* if */
 2434                                       mxMemUnitPtr = mxMemUnitPtr + PRINTF_SIZE;
 2435                                       break;
 2436                                   }
 2437                                   case RESPONSE_FOPEN: {
 2438                                       uint16_T FOPEN_FID;
 2439                                       uint16_T FOPEN_NAME_SIZE;
 2440                                       targetIOFd_T *targetIOFdPtr;
 2441                                       {
 2442                                           uint8_T * simDataMemUnitPtr;
 2443                                           simDataMemUnitPtr = (uint8_T *) &FOPEN_FID;
 2444                                           {
 2445                                               size_t num_elements = 1;
 2446                                               size_t bytes_consumed = num_elements * sizeof(uint16_T);
 2447                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2448                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2449                                           }
 2450                                       }
 2451                                       {
 2452                                           uint8_T * simDataMemUnitPtr;
 2453                                           simDataMemUnitPtr = (uint8_T *) &FOPEN_NAME_SIZE;
 2454                                           {
 2455                                               size_t num_elements = 1;
 2456                                               size_t bytes_consumed = num_elements * sizeof(uint16_T);
 2457                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2458                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2459                                           }
 2460                                       }
 2461                                       targetIOFdPtr = (targetIOFd_T *) ssGetPWorkValue(S, 2);
 2462                                       if (targetIOFdPtr != NULL) {
 2463                                           /* check fid increments by 1 */
 2464                                           if (targetIOFdPtr->fidOffset + 1 == FOPEN_FID) {
 2465                                               targetIOFdPtr->fidOffset = FOPEN_FID;
 2466                                               growTargetIOFd(S, targetIOFdPtr, targetIOFdPtr->fidOffset + 1);
 2467                                               if (targetIOFdPtr->Fd != NULL)  {
 2468                                                   uint8_T *pFopenBuff;
 2469                                                   targetIOFdPtr->Fd[targetIOFdPtr->fidOffset] = NULL;
 2470                                                   pFopenBuff = mxMemUnitPtr;
 2471                                                   if (pFopenBuff[FOPEN_NAME_SIZE-1] == '\0') {
 2472                                                       FILE * tmpFd = NULL;
 2473                                                       tmpFd = fopen((char *) pFopenBuff,"w");
 2474                                                       if (tmpFd != NULL) {
 2475                                                           /* save the file descriptor */
 2476                                                           targetIOFdPtr->Fd[targetIOFdPtr->fidOffset] = tmpFd;
 2477                                                       } else {
 2478                                                           {
 2479                                                               mxArray * prhs[ 2 ];
 2480                                                               prhs[0] = mxCreateString("PIL:pil:TargetIOFopenError");
 2481                                                               prhs[1] = mxCreateString((char *) pFopenBuff);
 2482                                                               callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 2483                                                               return;
 2484                                                           }
 2485                                                       } /* if */
 2486                                                   } /* if */
 2487                                               } /* if */
 2488                                           } else {
 2489                                               {
 2490                                                   mxArray * prhs[ 2 ];
 2491                                                   prhs[0] = mxCreateString("PIL:pil:TargetIOFopenFidError");
 2492                                                   prhs[1] = mxCreateDoubleScalar(FOPEN_FID);
 2493                                                   callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 2494                                                   return;
 2495                                               }
 2496                                           } /* if */
 2497                                       } /* if */
 2498                                       mxMemUnitPtr = mxMemUnitPtr + FOPEN_NAME_SIZE;
 2499                                       break;
 2500                                   }
 2501                                   case RESPONSE_FPRINTF: {
 2502                                       uint8_T FPRINTF_ERROR;
 2503                                       uint16_T FPRINTF_FID;
 2504                                       uint16_T FPRINTF_SIZE;
 2505                                       {
 2506                                           uint8_T * simDataMemUnitPtr;
 2507                                           simDataMemUnitPtr = (uint8_T *) &FPRINTF_ERROR;
 2508                                           {
 2509                                               size_t num_elements = 1;
 2510                                               size_t bytes_consumed = num_elements * sizeof(uint8_T);
 2511                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2512                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2513                                           }
 2514                                       }
 2515                                       {
 2516                                           uint8_T * simDataMemUnitPtr;
 2517                                           simDataMemUnitPtr = (uint8_T *) &FPRINTF_FID;
 2518                                           {
 2519                                               size_t num_elements = 1;
 2520                                               size_t bytes_consumed = num_elements * sizeof(uint16_T);
 2521                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2522                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2523                                           }
 2524                                       }
 2525                                       {
 2526                                           uint8_T * simDataMemUnitPtr;
 2527                                           simDataMemUnitPtr = (uint8_T *) &FPRINTF_SIZE;
 2528                                           {
 2529                                               size_t num_elements = 1;
 2530                                               size_t bytes_consumed = num_elements * sizeof(uint16_T);
 2531                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2532                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2533                                           }
 2534                                       }
 2535                                       if (FPRINTF_ERROR != TARGET_IO_SUCCESS) {
 2536                                           {
 2537                                               mxArray * prhs[ 2 ];
 2538                                               prhs[0] = mxCreateString("PIL:pil:TargetIOError");
 2539                                               prhs[1] = mxCreateDoubleScalar(FPRINTF_ERROR);
 2540                                               callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 2541                                               return;
 2542                                           }
 2543                                       } else {
 2544                                           targetIOFd_T * targetIOFdPtr = (targetIOFd_T *) ssGetPWorkValue(S, 2);
 2545                                           if (targetIOFdPtr != NULL) {
 2546                                               if (targetIOFdPtr->size > FPRINTF_FID) {
 2547                                                   if (targetIOFdPtr->Fd[FPRINTF_FID] != NULL) {
 2548                                                       uint8_T *pFprintfBuff;
 2549                                                       pFprintfBuff = mxMemUnitPtr;
 2550                                                       if (pFprintfBuff[FPRINTF_SIZE-1] == '\0') {
 2551                                                           fprintf(targetIOFdPtr->Fd[FPRINTF_FID], "%s", pFprintfBuff);
 2552                                                       } /* if */
 2553                                                   } /* if */
 2554                                               } /* if */
 2555                                           } /* if */
 2556                                       } /* if */
 2557                                       mxMemUnitPtr = mxMemUnitPtr + FPRINTF_SIZE;
 2558                                       break;
 2559                                   }
 2560                                   case RESPONSE_OUTPUT_DATA: {
 2561                                       {
 2562                                           uint8_T * simDataMemUnitPtr;
 2563                                           simDataMemUnitPtr = ( uint8_T *) ssGetOutputPortSignal(S, 0);
 2564                                           {
 2565                                               size_t num_elements = 1;
 2566                                               size_t bytes_consumed = num_elements * sizeof(real64_T);
 2567                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2568                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2569                                           }
 2570                                       }
 2571                                       {
 2572                                           uint8_T * simDataMemUnitPtr;
 2573                                           simDataMemUnitPtr = ( uint8_T *) ssGetOutputPortSignal(S, 1);
 2574                                           {
 2575                                               size_t num_elements = 1;
 2576                                               size_t bytes_consumed = num_elements * sizeof(real64_T);
 2577                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2578                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2579                                           }
 2580                                       }
 2581                                       {
 2582                                           uint8_T * simDataMemUnitPtr;
 2583                                           simDataMemUnitPtr = ( uint8_T *) ssGetOutputPortSignal(S, 2);
 2584                                           {
 2585                                               size_t num_elements = 1;
 2586                                               size_t bytes_consumed = num_elements * sizeof(real64_T);
 2587                                               memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2588                                               mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2589                                           }
 2590                                       }
 2591                                       break;
 2592                                   }
 2593                                   default: {
 2594                                       {
 2595                                           mxArray * prhs[ 2 ];
 2596                                           prhs[0] = mxCreateString("PIL:pilverification:UnknownResponseId");
 2597                                           prhs[1] = mxCreateDoubleScalar(responseId);
 2598                                           callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 0);
 2599                                           return;
 2600                                       }
 2601                                       break;
 2602                                   }
 2603                               } /* switch */
 2604                           } /* while */
 2605                       } /* if */
 2606                   } /* if */
 2607               } /* if */
 2608           }
 2609       } /* if */
 2610   }
 2611   
 2612   static void mdlTerminate(SimStruct *S) {
 2613       int isOkToTerminate;
 2614       int commErrorOccurred = 0;
 2615   
 2616       {
 2617           if (ssGetPWork(S) != NULL) {
 2618               int * pCommErrorOccurred = (int *) ssGetPWorkValue(S, 3);
 2619               if (pCommErrorOccurred != NULL) {
 2620                   commErrorOccurred = *pCommErrorOccurred;
 2621               } /* if */
 2622           } /* if */
 2623       }
 2624   
 2625       {
 2626           {
 2627               mxArray *rhs[3];
 2628               mxArray *lhs[1];
 2629               char * simulinkBlockPath = getSimulinkBlockPath(S);
 2630               if (simulinkBlockPath == NULL) {
 2631                   ssSetErrorStatus(S, "ModelBlock SIL/PIL unexpected error: getSimulinkBlockPath returned NULL pointer. Check search string was found in ssGetPath.\n");
 2632                   return;
 2633               }
 2634               rhs[ 0 ] = mxCreateString("@coder.connectivity.SimulinkInterface.getSILPILInterface");
 2635               rhs[ 1 ] = mxCreateDoubleScalar( 1 );
 2636               rhs[ 2 ] = mxCreateString(simulinkBlockPath);
 2637               callMATLAB(S,  1, lhs, 3, rhs, "rtw.pil.SILPILInterface.sfunctionIsOkToTerminateHook", 0);
 2638               mxFree((void *) simulinkBlockPath);
 2639               isOkToTerminate = (int) (*mxGetPr(lhs[0]));
 2640               mxDestroyArray(lhs[0]);
 2641           }
 2642       }
 2643       if (isOkToTerminate) {
 2644           if (!commErrorOccurred) {
 2645               {
 2646                   uint8_T * mxMemUnitPtr;
 2647                   mwSize dataInSize = 0;
 2648                   IOBuffer_T * IOBufferPtr = (IOBuffer_T *) ssGetPWorkValue(S, 1);
 2649                   if (IOBufferPtr != NULL) {
 2650                       growIOBuffer(S, IOBufferPtr, 5);
 2651                       if (IOBufferPtr->data != NULL) {
 2652                           mxMemUnitPtr = (uint8_T *) IOBufferPtr->data;
 2653                           /* write command id */
 2654                           *mxMemUnitPtr = 4;
 2655                           mxMemUnitPtr++;
 2656                           {
 2657                               uint8_T * simDataMemUnitPtr;
 2658                               uint32_T commandDataFcnid = 0;
 2659                               simDataMemUnitPtr = (uint8_T *) &commandDataFcnid;
 2660                               {
 2661                                   size_t num_elements = 1;
 2662                                   size_t bytes_consumed = num_elements * sizeof(uint32_T);
 2663                                   memcpy((void *) mxMemUnitPtr, (void *) simDataMemUnitPtr, bytes_consumed);
 2664                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2665                               }
 2666                           }
 2667                           {
 2668                               int errorOccurred;
 2669                               errorOccurred = commandDispatch(S, IOBufferPtr, 5, &dataInSize, 1);
 2670                               if (errorOccurred) {
 2671                                   /* continue */
 2672                               } /* if */
 2673                           }
 2674                           if (dataInSize > 0) {
 2675                               size_t dataInMemUnitSize = dataInSize;
 2676                               uint8_T responseId;
 2677                               uint8_T * mxMemUnitPtrEnd;
 2678                               #define RESPONSE_ERROR 0
 2679                               #define RESPONSE_PRINTF 3
 2680                               #define RESPONSE_FOPEN 4
 2681                               #define RESPONSE_FPRINTF 5
 2682                               #define TARGET_IO_SUCCESS 0
 2683                               mxMemUnitPtr = (uint8_T *) IOBufferPtr->data;
 2684                               mxMemUnitPtrEnd = mxMemUnitPtr + dataInMemUnitSize - 1;
 2685                               while (mxMemUnitPtr <= mxMemUnitPtrEnd) {
 2686                                   /* read response id */
 2687                                   responseId = *mxMemUnitPtr;
 2688                                   mxMemUnitPtr++;
 2689                                   switch (responseId) {
 2690                                       case RESPONSE_ERROR: {
 2691                                           uint8_T errorId = *mxMemUnitPtr;
 2692                                           mxMemUnitPtr++;
 2693                                           if (errorId) {
 2694                                               {
 2695                                                   mxArray * prhs[ 2 ];
 2696                                                   prhs[0] = mxCreateString("PIL:pilverification:PILError");
 2697                                                   prhs[1] = mxCreateDoubleScalar(errorId);
 2698                                                   callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 1);
 2699                                                   /* continue */
 2700                                               }
 2701                                           } /* if */
 2702                                           break;
 2703                                       }
 2704                                       case RESPONSE_PRINTF: {
 2705                                           uint8_T PRINTF_ERROR;
 2706                                           uint16_T PRINTF_SIZE;
 2707                                           {
 2708                                               uint8_T * simDataMemUnitPtr;
 2709                                               simDataMemUnitPtr = (uint8_T *) &PRINTF_ERROR;
 2710                                               {
 2711                                                   size_t num_elements = 1;
 2712                                                   size_t bytes_consumed = num_elements * sizeof(uint8_T);
 2713                                                   memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2714                                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2715                                               }
 2716                                           }
 2717                                           {
 2718                                               uint8_T * simDataMemUnitPtr;
 2719                                               simDataMemUnitPtr = (uint8_T *) &PRINTF_SIZE;
 2720                                               {
 2721                                                   size_t num_elements = 1;
 2722                                                   size_t bytes_consumed = num_elements * sizeof(uint16_T);
 2723                                                   memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2724                                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2725                                               }
 2726                                           }
 2727                                           if (PRINTF_ERROR != TARGET_IO_SUCCESS) {
 2728                                               {
 2729                                                   mxArray * prhs[ 2 ];
 2730                                                   prhs[0] = mxCreateString("PIL:pil:TargetIOError");
 2731                                                   prhs[1] = mxCreateDoubleScalar(PRINTF_ERROR);
 2732                                                   callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 1);
 2733                                                   /* continue */
 2734                                               }
 2735                                           } else {
 2736                                               uint8_T *pPrintBuff;
 2737                                               pPrintBuff = mxMemUnitPtr;
 2738                                               if (pPrintBuff[PRINTF_SIZE-1] == '\0') {
 2739                                                   mexPrintf("%s", pPrintBuff);
 2740                                               } /* if */
 2741                                           } /* if */
 2742                                           mxMemUnitPtr = mxMemUnitPtr + PRINTF_SIZE;
 2743                                           break;
 2744                                       }
 2745                                       case RESPONSE_FOPEN: {
 2746                                           uint16_T FOPEN_FID;
 2747                                           uint16_T FOPEN_NAME_SIZE;
 2748                                           targetIOFd_T *targetIOFdPtr;
 2749                                           {
 2750                                               uint8_T * simDataMemUnitPtr;
 2751                                               simDataMemUnitPtr = (uint8_T *) &FOPEN_FID;
 2752                                               {
 2753                                                   size_t num_elements = 1;
 2754                                                   size_t bytes_consumed = num_elements * sizeof(uint16_T);
 2755                                                   memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2756                                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2757                                               }
 2758                                           }
 2759                                           {
 2760                                               uint8_T * simDataMemUnitPtr;
 2761                                               simDataMemUnitPtr = (uint8_T *) &FOPEN_NAME_SIZE;
 2762                                               {
 2763                                                   size_t num_elements = 1;
 2764                                                   size_t bytes_consumed = num_elements * sizeof(uint16_T);
 2765                                                   memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2766                                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2767                                               }
 2768                                           }
 2769                                           targetIOFdPtr = (targetIOFd_T *) ssGetPWorkValue(S, 2);
 2770                                           if (targetIOFdPtr != NULL) {
 2771                                               /* check fid increments by 1 */
 2772                                               if (targetIOFdPtr->fidOffset + 1 == FOPEN_FID) {
 2773                                                   targetIOFdPtr->fidOffset = FOPEN_FID;
 2774                                                   growTargetIOFd(S, targetIOFdPtr, targetIOFdPtr->fidOffset + 1);
 2775                                                   if (targetIOFdPtr->Fd != NULL)  {
 2776                                                       uint8_T *pFopenBuff;
 2777                                                       targetIOFdPtr->Fd[targetIOFdPtr->fidOffset] = NULL;
 2778                                                       pFopenBuff = mxMemUnitPtr;
 2779                                                       if (pFopenBuff[FOPEN_NAME_SIZE-1] == '\0') {
 2780                                                           FILE * tmpFd = NULL;
 2781                                                           tmpFd = fopen((char *) pFopenBuff,"w");
 2782                                                           if (tmpFd != NULL) {
 2783                                                               /* save the file descriptor */
 2784                                                               targetIOFdPtr->Fd[targetIOFdPtr->fidOffset] = tmpFd;
 2785                                                           } else {
 2786                                                               {
 2787                                                                   mxArray * prhs[ 2 ];
 2788                                                                   prhs[0] = mxCreateString("PIL:pil:TargetIOFopenError");
 2789                                                                   prhs[1] = mxCreateString((char *) pFopenBuff);
 2790                                                                   callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 1);
 2791                                                                   /* continue */
 2792                                                               }
 2793                                                           } /* if */
 2794                                                       } /* if */
 2795                                                   } /* if */
 2796                                               } else {
 2797                                                   {
 2798                                                       mxArray * prhs[ 2 ];
 2799                                                       prhs[0] = mxCreateString("PIL:pil:TargetIOFopenFidError");
 2800                                                       prhs[1] = mxCreateDoubleScalar(FOPEN_FID);
 2801                                                       callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 1);
 2802                                                       /* continue */
 2803                                                   }
 2804                                               } /* if */
 2805                                           } /* if */
 2806                                           mxMemUnitPtr = mxMemUnitPtr + FOPEN_NAME_SIZE;
 2807                                           break;
 2808                                       }
 2809                                       case RESPONSE_FPRINTF: {
 2810                                           uint8_T FPRINTF_ERROR;
 2811                                           uint16_T FPRINTF_FID;
 2812                                           uint16_T FPRINTF_SIZE;
 2813                                           {
 2814                                               uint8_T * simDataMemUnitPtr;
 2815                                               simDataMemUnitPtr = (uint8_T *) &FPRINTF_ERROR;
 2816                                               {
 2817                                                   size_t num_elements = 1;
 2818                                                   size_t bytes_consumed = num_elements * sizeof(uint8_T);
 2819                                                   memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2820                                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2821                                               }
 2822                                           }
 2823                                           {
 2824                                               uint8_T * simDataMemUnitPtr;
 2825                                               simDataMemUnitPtr = (uint8_T *) &FPRINTF_FID;
 2826                                               {
 2827                                                   size_t num_elements = 1;
 2828                                                   size_t bytes_consumed = num_elements * sizeof(uint16_T);
 2829                                                   memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2830                                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2831                                               }
 2832                                           }
 2833                                           {
 2834                                               uint8_T * simDataMemUnitPtr;
 2835                                               simDataMemUnitPtr = (uint8_T *) &FPRINTF_SIZE;
 2836                                               {
 2837                                                   size_t num_elements = 1;
 2838                                                   size_t bytes_consumed = num_elements * sizeof(uint16_T);
 2839                                                   memcpy((void *) simDataMemUnitPtr, (void *) mxMemUnitPtr, bytes_consumed);
 2840                                                   mxMemUnitPtr+=(bytes_consumed/sizeof(*mxMemUnitPtr));
 2841                                               }
 2842                                           }
 2843                                           if (FPRINTF_ERROR != TARGET_IO_SUCCESS) {
 2844                                               {
 2845                                                   mxArray * prhs[ 2 ];
 2846                                                   prhs[0] = mxCreateString("PIL:pil:TargetIOError");
 2847                                                   prhs[1] = mxCreateDoubleScalar(FPRINTF_ERROR);
 2848                                                   callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 1);
 2849                                                   /* continue */
 2850                                               }
 2851                                           } else {
 2852                                               targetIOFd_T * targetIOFdPtr = (targetIOFd_T *) ssGetPWorkValue(S, 2);
 2853                                               if (targetIOFdPtr != NULL) {
 2854                                                   if (targetIOFdPtr->size > FPRINTF_FID) {
 2855                                                       if (targetIOFdPtr->Fd[FPRINTF_FID] != NULL) {
 2856                                                           uint8_T *pFprintfBuff;
 2857                                                           pFprintfBuff = mxMemUnitPtr;
 2858                                                           if (pFprintfBuff[FPRINTF_SIZE-1] == '\0') {
 2859                                                               fprintf(targetIOFdPtr->Fd[FPRINTF_FID], "%s", pFprintfBuff);
 2860                                                           } /* if */
 2861                                                       } /* if */
 2862                                                   } /* if */
 2863                                               } /* if */
 2864                                           } /* if */
 2865                                           mxMemUnitPtr = mxMemUnitPtr + FPRINTF_SIZE;
 2866                                           break;
 2867                                       }
 2868                                       default: {
 2869                                           {
 2870                                               mxArray * prhs[ 2 ];
 2871                                               prhs[0] = mxCreateString("PIL:pilverification:UnknownResponseId");
 2872                                               prhs[1] = mxCreateDoubleScalar(responseId);
 2873                                               callMATLAB(S,  0, NULL, 2, prhs, "DAStudio.error", 1);
 2874                                               /* continue */
 2875                                           }
 2876                                           break;
 2877                                       }
 2878                                   } /* switch */
 2879                               } /* while */
 2880                           } /* if */
 2881                       } /* if */
 2882                   } /* if */
 2883               }
 2884           } /* if */
 2885           closeAndFreeTargetIOFd(S);
 2886           {
 2887               {
 2888                   mxArray *rhs[3];
 2889                   char * simulinkBlockPath = getSimulinkBlockPath(S);
 2890                   if (simulinkBlockPath == NULL) {
 2891                       ssSetErrorStatus(S, "ModelBlock SIL/PIL unexpected error: getSimulinkBlockPath returned NULL pointer. Check search string was found in ssGetPath.\n");
 2892                       return;
 2893                   }
 2894                   rhs[ 0 ] = mxCreateString("@coder.connectivity.SimulinkInterface.getSILPILInterface");
 2895                   rhs[ 1 ] = mxCreateDoubleScalar( 1 );
 2896                   rhs[ 2 ] = mxCreateString(simulinkBlockPath);
 2897                   callMATLAB(S,  0, NULL, 3, rhs, "rtw.pil.SILPILInterface.sfunctionPILStopHook", 0);
 2898                   mxFree((void *) simulinkBlockPath);
 2899               }
 2900           }
 2901       } /* if */
 2902       if (ssGetPWork(S) != NULL) {
 2903           IOBuffer_T * IOBufferPtr;
 2904           rtIOStreamData * rtIOStreamDataPtr = (rtIOStreamData *) ssGetPWorkValue(S, 0);
 2905           if (rtIOStreamDataPtr != NULL) {
 2906               {
 2907                   int errorStatus = rtIOStreamUnloadLib(&rtIOStreamDataPtr->libH);
 2908                   if (errorStatus) {
 2909                       ssSetErrorStatus( S,"rtIOStreamUnloadLib failed.");
 2910                   } /* if */
 2911               }
 2912               mxFree(rtIOStreamDataPtr->lib);
 2913               mxDestroyArray(rtIOStreamDataPtr->MATLABObject);
 2914               mxFree(rtIOStreamDataPtr);
 2915               ssSetPWorkValue(S, 0, NULL);
 2916           } /* if */
 2917           IOBufferPtr = (IOBuffer_T *) ssGetPWorkValue(S, 1);
 2918           if (IOBufferPtr != NULL) {
 2919               mxFree(IOBufferPtr->data);
 2920               mxFree(IOBufferPtr);
 2921               ssSetPWorkValue(S, 1, NULL);
 2922           } /* if */
 2923           closeAndFreeTargetIOFd(S);
 2924       } /* if */
 2925   }
 2926   
 2927   #define MDL_ENABLE
 2928   #if defined(MDL_ENABLE)
 2929   static void mdlEnable(SimStruct *S) {
 2930   }
 2931   #endif /* MDL_ENABLE */
 2932   
 2933   #define MDL_DISABLE
 2934   #if defined(MDL_DISABLE)
 2935   static void mdlDisable(SimStruct *S) {
 2936   }
 2937   #endif /* MDL_DISABLE */
 2938   
 2939   /* Required S-function trailer */
 2940   #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
 2941   #include "simulink.c" /* MEX-file interface mechanism */
 2942   #include "fixedpoint.c"
 2943   #else
 2944   #error Assertion failed: file must be compiled as a MEX-file
 2945   #endif
 2946   
 2947